ImageView has 4 APIs to specify the image.
- setImageDrawable(Drawable drawable)
- setImageBitmap(Bitmap bm)
- setImageResource(int resId)
- 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
Strange out of memory issue while loading an image to a Bitmap object
Android developer documentation: https://developer.android.com/training/displaying-bitmaps/load-bitmap.html