gpt4 book ai didi

android - 如何下载多张图片并像幻灯片一样显示?

转载 作者:行者123 更新时间:2023-11-30 04:29:57 26 4
gpt4 key购买 nike

嘿,我是 android 世界的新手,正在考虑制作一个小应用程序,它将多个图像(可能从指定的 url)下载到 sdcard 或下载文件夹中,然后这些图像可以被视为幻灯片(有按钮下一个和后面)。另外,在下载过程中应该有一个进度条来显示下载进度。我可以为一张图片做,但不能为多张图片做。这是我的代码:

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的新手,所以不太了解。谢谢

最佳答案

使用画廊小工具。您需要制作一个适配器,为 Gallery-Widget 提供您下载的图像。我确定您可以在此页面或 Google 上找到有关如何实现图库的代码;)

实现画廊后,有几种方法可以在图像之间自动翻转,下面是一个示例:

Using the Android Gallery as an automated slideshow

关于android - 如何下载多张图片并像幻灯片一样显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7900298/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com