1

I have 3 or 4 image paths that I use to load an image so I set it to an imageview. Why does it take long? Or better asking is there a way to make it faster? At the end of the day I am loading to fit an imageview of less than 60 dp hight and width

Uri mainImgeUri = Uri.parse(imagePath);
InputStream imageStream;
try {
    imageStream = mActiviy.getContentResolver().openInputStream(mainImgeUri);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 8;
    Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream, null, options);
    mainImageIV.setImageBitmap(yourSelectedImage);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

USE CASE: What happens is that a user will add 5 images (and he get to choose them from Gallery which is mostly taken by phone camera). He hit save and my app stores the path to them in an sqlite database. Then when the user opens the app again to see them, my app query the db to get the paths to all the images and executes the code above x number of times so all the image views are loaded with the intended images

Snake
  • 14,228
  • 27
  • 117
  • 250
  • 1
    Define slow. I have an app with dozens of static images. I can swipe through them as fast as I can move my finger. I suspect it's the contentResolver introducing the delay. – Simon Feb 24 '13 at 22:37
  • How large are the images you are trying to load? Also, what is the ImageView being used for? – Victor KP Feb 24 '13 at 22:38
  • I am already using AncyTsk. The images are taken by the phone camera. Then they are loaded into little images views – Snake Feb 24 '13 at 22:46
  • 2
    So you are loading images in the range of 5-8 megapixels for a tiny 60dp ImageView? – Victor KP Feb 24 '13 at 23:33
  • http://stackoverflow.com/questions/14198949/android-efficient-way-to-load-multiple-images-from-remote-server – Robert Rowntree Feb 25 '13 at 00:19
  • @Victor KP, yes any other better way? – Snake Feb 25 '13 at 01:33
  • Thanks Robert, but not a concrete answer – Snake Feb 25 '13 at 01:34
  • @Snake Save smaller sample sizes and only load the most appropriate size. This is a common technique used by many programs/systems to greatly reduce the bandwidth/size when not required. During the thumbnail generate a good down-sample algorithm can also be used. –  Feb 25 '13 at 01:38
  • So I should create a local folder and save an copy/downsized sample of the image to it? Is not this a waste of memory/storage ! – Snake Feb 25 '13 at 01:46
  • @Snake The small images will take up *virtual no* space in comparison to the full image, also PNG is much more efficient than BMP space-wise and is very suitable for this task. So no, it's not a waste. –  Feb 25 '13 at 01:47
  • @Snake Oh, disregard the SQLite stuff (now removed), I thought that it was being used to store the image data as well. But yes, a local cache folder will further help by reducing network usage - might not even need to download anything if the image was pushed from that device. Anyway, consider that 8GB is now "standard amounts of storage" on newer devices and someone who pushes 8MP+ images isn't exactly on old hardware. Could also make the cache configurable or whatnot. –  Feb 25 '13 at 01:52
  • hmm, ok Any tutorial or example on how to Achieve this pst? – Snake Feb 25 '13 at 01:56
  • @Snake Not that I know of, although it doesn't seem that uncommon a task. –  Feb 25 '13 at 01:58

1 Answers1

0

Take a look at http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

It explains how to calculate the correct inSampleSize based on the required dimensions of the output image. It also explains how to reference large bitmaps without having to load all their pixel data into memory.

The idea is that you resample bigger images and only load the smaller ones into memory making the whole process much more efficient. The example code is accessing a bitmap from resources, but this can easily be modified for your needs.

The important things to look out for in the example are inJustDecodeBounds and calculateInSampleSize.

speedynomads
  • 2,632
  • 1
  • 26
  • 24