Android 异步任务中Doinbackground中的for循环不递增
Im使用for循环从doInBackground中的数组列表中读取图像url,但循环并没有递增,只有第一个url正在加载和保存图像。以下是代码:Android 异步任务中Doinbackground中的for循环不递增,android,android-asynctask,android-implicit-intent,Android,Android Asynctask,Android Implicit Intent,Im使用for循环从doInBackground中的数组列表中读取图像url,但循环并没有递增,只有第一个url正在加载和保存图像。以下是代码: class DownloadImageTask extends AsyncTask<Void, Void, Bitmap> { // This class definition states that DownloadImageTask will take String // parameters, publish Inte
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;
}
}
}
class DownloadImageTask扩展了AsyncTask{
//该类定义声明DownloadImageTask将采用字符串
//参数,发布整数进度更新,并返回位图
@抑制警告(“未使用”)
受保护位图doInBackground(无效…路径){
//网址;
试一试{
对于(int j=0;jList
是一个接口,您不能使用likeList.size()
。请定义一个列表子类型,如ArrayList或其他类型。for循环没有问题。例如:
List<Integer> li = new ArrayList<Integer>();
li.add(10);
li.add(20)';
for(int i = 0; i <= li.size(); ++i) {
//your stuff
}
List li=new ArrayList();
加(10);
li.加入(20)‘;
对于(int i=0;iList
是一个接口,您不能使用likeList.size()
。请定义一个列表子类型,如ArrayList或其他类型。for循环没有问题。例如:
List<Integer> li = new ArrayList<Integer>();
li.add(10);
li.add(20)';
for(int i = 0; i <= li.size(); ++i) {
//your stuff
}
List li=new ArrayList();
加(10);
li.加入(20)‘;
对于(int i=0;我认为返回位图;
中断循环,我认为List.size()是的,当我评论return bitmap时,语句循环运行得很好,但是我可以返回到哪里?@BibiTahira:将您的代码更改为:创建位图类型的ArrayList,并将return bitmap;
替换为ArrayList.add(位图);
并最终通过替换返回null来返回位图数组列表而不是null;
@bibibitahira:为什么不调用saveToSDCard(result,name);
内部doInBackground方法?我认为返回位图;
以某种方式破坏循环我认为列表。size()是的,当我评论return bitmap时,语句循环运行得很好,但是我可以返回到哪里?@BibiTahira:将您的代码更改为:创建位图类型的ArrayList,并将return bitmap;
替换为ArrayList.add(位图);
并最终通过替换返回null来返回位图数组列表而不是null;
@bibibitahira:为什么不调用saveToSDCard(result,name);
内部doInBackground方法?