Sobhan Moradi
Sobhan Moradi

Reputation: 569

Load 10 image on first page

I use this method to load 10 image on first page but i have one problem. first image takes a lot of time to load. After first image other image loading fast. I don`t know why?

calling method new DownloadImageTask((ImageView) findViewById(R.id.imageView_bb1).execute(db.getURLimgMahsol(id_bb2));

loadImage method

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;

        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }
    }

Upvotes: 0

Views: 127

Answers (3)

Gueorgui Obregon
Gueorgui Obregon

Reputation: 5087

Check that you call DownloadImageTask with execute() and not with get(). Executing get() will block your UI until the task is finished.

Please DON'T reinvent the wheel! There is many good library to handle efficiently loading an image from url.

Hope this helps!!

Upvotes: 1

Volodymyr Kulyk
Volodymyr Kulyk

Reputation: 6546

Probably you start 10 AsyncTask's one by one, so UI gets freeze.
You should make queue: start first AsyncTask -> onPostExecute start second and so on.


I suggest you to use Glide.
Here is the discussion about Glide and other ImageLoaders.

Upvotes: -1

Vladimir Jovanović
Vladimir Jovanović

Reputation: 2267

Use Glide, Picasso or some other image loader. Also it's not good practice to hold reference on UI element in AsyncTask, or to have private inner AsyncTask class. You can see here why.

Upvotes: 1

Related Questions