gpt4 book ai didi

android - for循环在异步任务的Doinbackground中不递增

转载 作者:行者123 更新时间:2023-11-30 03:45:32 26 4
gpt4 key购买 nike

我正在使用 for 循环从 doInBackground 中的数组列表中读取图像 url,但循环不递增,只有第一个 url 正在加载和保存图像。这是代码:

class DownloadImageTask extends AsyncTask<Void, Void, Bitmap> {
// This class definition states that DownloadImageTask will take String
// parameters, publish Integer progress updates, and return a Bitmap
@SuppressWarnings("unused")
protected Bitmap doInBackground(Void... paths) {
//URL url;
try {
for(int j=0; j<List.size();j++)
{
reviewImageLink =List.get(j).get(TAG_Image);
URL url = new URL(reviewImageLink);
// URL reviewImageURL;
String name = reviewImageLink.substring(reviewImageLink .lastIndexOf("/") + 1,reviewImageLink.length());
//try {

if (!hasExternalStoragePublicPicture(name)) {
isImage = false;
//new DownloadImageTask().execute();
Log.v("log_tag", "if");
isImage = true;
File sdImageMainDirectory = new File(Environment.getExternalStorageDirectory(), getResources().getString(R.string.directory));
//if(!sdImageMainDirectory.exists()){
sdImageMainDirectory.mkdirs();
File file = new File(sdImageMainDirectory, name);
Log.v("log_tag", "Directory created");}
//}
// catch (MalformedURLException e) {
// Log.v(TAG, e.toString()); }
// }

// }//try
// catch (Exception e) {
// e.printStackTrace();}
//url = new URL(List.get(j).get(TAG_Image));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int length = connection.getContentLength();
InputStream is = (InputStream) url.getContent();
byte[] imageData = new byte[length];
int buffersize = (int) Math.ceil(length / (double) 100);
int downloaded = 0;
int read;
while (downloaded < length) {
if (length < buffersize) {
read = is.read(imageData, downloaded, length);
} else if ((length - downloaded) <= buffersize) {
read = is.read(imageData, downloaded, length- downloaded);
} else {
read = is.read(imageData, downloaded, buffersize);
}
downloaded += read;
setProgress((downloaded * 100) / length);
}
Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0,length);
if (bitmap != null) {
Log.i(TAG, "Bitmap created");
} else {
Log.i(TAG, "Bitmap not created");
}
is.close();
return bitmap;

}
}catch (MalformedURLException e) {
Log.e(TAG, "Malformed exception: " + e.toString());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.toString());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.toString());
}
return null;

}

protected void onPostExecute(Bitmap result) {
String name = reviewImageLink.substring(reviewImageLink.lastIndexOf("/") + 1,reviewImageLink.length());
if (result != null) {
hasExternalStoragePublicPicture(name);
saveToSDCard(result, name);
isImage = true;

} else {
isImage = false;

}
}
}

最佳答案

List是一个接口(interface),不能像List.size()那样使用。定义一个List子类型,比如ArrayList之类的。for循环没有错. 例如:

List<Integer> li = new ArrayList<Integer>();
li.add(10);
li.add(20)';
for(int i = 0; i <= li.size(); ++i) {
//your stuff
}

关于android - for循环在异步任务的Doinbackground中不递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15061715/

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