0

I use the "imgUrl" to save the URL where I download the image and "BitmapFactory" to zoom out and not waste RAM, but it takes a lot because there are several pictures, Do you know a better way?.

public class DownloadImage extends AsyncTask<Object, Void, Bitmap> {
        ImageView imagen;
        String imgUrl="";
        Bitmap bitm;

        protected Bitmap doInBackground(Object... params){
            imgUrl = (String) params[0];
            imagen =  (ImageView) params[1];
            try {

                URL imageUrl = new URL(imgUrl); /*Using the URL for download the image*/

                HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
                conn.connect();



                BitmapFactory.Options options = new BitmapFactory.Options(); 
                options.inSampleSize = 3;

                bitm = BitmapFactory.decodeStream(conn.getInputStream(), new Rect(0, 0, 0, 0), options);
            } catch (IOException e) {
                Log.e("catch", e+"");
            }
            return bitm;
        }

        protected void onPostExecute(Bitmap result){
            imagen.setImageBitmap(result);
            super.onPostExecute(result);
        }
    }
  • Have you had a look at these 2 posts before ? Will get you some ideas http://stackoverflow.com/questions/6589051/android-correct-multithreading and http://stackoverflow.com/questions/22183000/android-faster-download-of-files-with-multi-thread – JustWe May 05 '14 at 23:46

1 Answers1

2

Not sure what do you want to optimize, but there are several ways to optimize the images used in an app which are fetched online.

  1. If images are large and your app will show different sizes of it, you can store different sizes of the same image on the server, and based on the need you can download the size you need. This saves in data transfer, and makes the image loading (e.g. in lists which thumbnails) faster and more smooth.

  2. You can obviously use a cache, usually a two level cache which uses both RAM and disk, to store downloaded image on the disk and not to download it again.

  3. You can use different APIs to analyze the image before loading and load the size you need. This way you load the image faster and use less memory in your view/cache to hold it.

You can read more details about this here

m.hashemian
  • 1,786
  • 2
  • 15
  • 31