W
W
wolfak2016-03-04 21:58:19
Java
wolfak, 2016-03-04 21:58:19

Why in android photos are loaded together and not one by one in AsyncTask?

Good evening. Faced a problem while developing a client application for android. When uploading photos from the site, the photos load but the images don't show up until they're all uploaded and all at once are abruptly displayed, and I would like them to start displaying one on one during the upload process. In addition, the application stops during the download process itself and nothing can be done in the application until the download is completed, but I would like that when the pictures are loaded, you can go to another page and cancel the download in this way, if necessary.
Below is the image upload code:

for(int i = 0; i < 15; i++) {
ImageView  PostSmallPhoto = (ImageView) findViewById(ArrIDPost[i]);
LoadImage GetContLoadImg = new LoadImage();
GetContLoadImg.execute("http://site.ru/img/" + i + ".jpg");
try {
bitmap = GetContLoadImg.get(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
} catch (ExecutionException e) {
} catch (TimeoutException e) {
}
if (bitmap != null) {
PostSmallPhoto.setImageBitmap(bitmap);
} else {
PostSmallPhoto.setBackgroundResource(R.drawable.nophoto);
}

bitmap = null;
GetContLoadImg = null;
}

private class LoadImage extends AsyncTask<String, String, Bitmap> {
@Override
protected void onPreExecute() {
super.onPreExecute();

}
protected Bitmap doInBackground(String... args) {
try {
bitmap = BitmapFactory.decodeStream((InputStream) new URL(args[0]).getContent());

} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}

protected void onPostExecute(Bitmap image) {

if(!isOnline()) {
Toast toast = Toast.makeText(getApplicationContext(), getResources().getString(R.string.ForAppNeIntr), Toast.LENGTH_SHORT);
toast.show();
} else {
if (image != null) {
bitmap = image;
}
}
}
}

How can I fix the code above so that the photos are loaded and displayed one by one instead of waiting for all the photos to load? And how to make the application not stop at the time of loading, but work as usual?
I hope for your help. Thanks a lot!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
one pavel, 2016-03-04
@wolfak

Respect to you!
Well done, because he was sitting writing, poking around and understanding!
Writing asynchronous image loading with smooth display
is not a trivial task. The code that you have to edit is better not worth it.
Take a proven library, where everything you need is implemented.
square.github.io/picasso
read habr https://habrahabr.ru/post/262189/
comparison of popular libraries
stackoverflow.com/questions/29363321/picasso-vsi...

A
Alexander, 2016-03-05
@itvdonsk

The advice about the pikasa is correct. But it also needs to be sorted out.
You just need to get images in asyntask one by one. Those. one image - one task. Those. the cycle needs to be taken out of the asyntask and in this cycle they actually run.

M
Makhach Imangazaliev, 2016-03-05
@ImangazalievM

Android library for loading Picasso images - java-help.ru/android-picasso

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question