简体   繁体   中英

Difference between URI and bitmap image

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 ?

在此处输入图片说明

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 (eg 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM