1

What is the best practice to display an image to imageView? We have two type of image, one is bitmap and another is URI. If I use bitmap,

Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);

the image is a bit blurry.

If I use URI, sometimes I get out of memory issues.

 URI imageUri = data.getData();
 imageView.setImageURI(imageURI);

What is the difference between the two ?

enter image description here

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
AI.
  • 934
  • 2
  • 14
  • 30

2 Answers2

3

Bitmap is a ready image (set of bytes with color data) and URI is a path to something. URI can be /emulated/home/... , can be http://google.com and so on.

Vinoth Vino
  • 9,166
  • 3
  • 66
  • 70
Ekalips
  • 1,473
  • 11
  • 20
2

ImageView has 4 APIs to specify the image.

  1. setImageDrawable(Drawable drawable)
  2. setImageBitmap(Bitmap bm)
  3. setImageResource(int resId)
  4. setImageURI(URI uri)

here setImageDrawable is the primitive function other APIs rely on. The other 3 are just helper methods making you write less code.

setImageURI, setImageBitmap both run on the UI thread. I would say setImageBitmap is bit faster than the first one. setImageURI really depends where the Uri resource comes from (e.g. the uri could point to a remote file not even stored on the phone).

setImageURI() is not better to use as reading and decoding on the UI thread, which can cause a latency hiccup.

Better to use the following:-

setImageDrawable(android.graphics.drawable.Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead.

you can also return bitmap from uri and use it in imageview

 Uri imageUri = intent.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
Imageview my_img_view = (Imageview ) findViewById (R.id.my_img_view);
my_img_view.setImageBitmap(bitmap);

Also sometime loading large bitmap on imageview can cause out of memory exception..so you should load bitmap efficiently..

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
    int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

mImageView.setImageBitmap(
    decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));

see those link also..for better understanding

  1. Strange out of memory issue while loading an image to a Bitmap object

  2. Android developer documentation: https://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Community
  • 1
  • 1
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • How do I know what is the value for reqWidth and reqHeight so the image will not look blurry ? – AI. Jan 12 '17 at 17:04