嘿,我是Android世界的新手,并且正在考虑制作一个小应用程序,它可以将多个图像(可能是从指定的URL)下载到sdcard或downloads文件夹中,然后这些图像可以看作幻灯片(有按钮)下一个和后一个)。另外,在下载过程中,应该有一个进度条来显示下载进度。我可以为一个图像而不是多个图像。 这是我的代码:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class lab1_dhruv extends Activity{
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private Button startBtn;
private ProgressDialog mProgressDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startBtn = (Button)findViewById(R.id.startBtn);
startBtn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
startDownload();
}
});
}
private void startDownload() {
//String url = "http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg";
//new DownloadFileAsync().execute(url);
String[] imageList = {"http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg","http://fc03.deviantart.net/fs14/i/2007/086/9/1/Steve_Jobs_portrait_by_tumb.jpg","http://www.buzzreactor.com/sites/default/files/Bill-Gates1.jpg"};
//String url1 = "http://fc03.deviantart.net/fs14/i/2007/086/9/1/Steve_Jobs_portrait_by_tumb.jpg";
new DownloadFileAsync().execute(imageList);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading file..");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
class DownloadFileAsync extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/some_photo_from_gdansk_poland.jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
}
public void showDownload(View view) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File("/sdcard/some_photo_from_gdansk_poland.jpg");
intent.setDataAndType(Uri.fromFile(file), "image/*");
startActivity(intent);
}
}
这是main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:text="Start the download here"
android:id="@+id/startBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button
android:text="Show Download Image"
android:id="@+id/startBtn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="showDownload">
</Button>
这里是androidmanifest.xml: -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cmpe235.lab1.dhruv.assignment"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".lab1_dhruv"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
是否有任何示例示例我可以看到或者任何人都可以告诉我应该在我的代码中做什么。我是android新手,所以不太了解。谢谢
答案 0 :(得分:1)
使用Gallery Widget。您需要使用下载的图像制作一个为Gallery-Widget提供服务的适配器。我相信您可以找到有关如何在此页面或Google上实施图库的代码;)
实施图库后,您可以通过多种方式在图像之间自动翻转,这是一个示例: