Android URI和位图图像之间的差异
将图像显示到imageView的最佳做法是什么?我们有两种类型的图像,一种是位图,另一种是URI。如果我使用位图Android URI和位图图像之间的差异,android,bitmap,imageview,uri,Android,Bitmap,Imageview,Uri,将图像显示到imageView的最佳做法是什么?我们有两种类型的图像,一种是位图,另一种是URI。如果我使用位图 Bitmap photo = (Bitmap) data.getExtras().get("data"); imageView.setImageBitmap(photo); 图像有点模糊 如果我使用URI,有时会出现内存不足的问题 URI imageUri = data.getData(); imageView.setImageURI(imageURI); 这两者的区别是什么
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
图像有点模糊
如果我使用URI,有时会出现内存不足的问题
URI imageUri = data.getData();
imageView.setImageURI(imageURI);
这两者的区别是什么
位图是包含颜色数据的字节的现成图像集,URI是指向某个对象的路径。URI可以是/simulated/home/,可以是等等。位图是一个包含颜色数据的字节的现成图像集,URI是指向某个内容的路径。URI可以是/simulated/home/,可以是等。ImageView有4个API来指定图像 SetImageDrawable可绘制可绘制 SetImageBitMap位图 setImageResourceint剩余 SetImageUri 这里setImageDrawable是其他API所依赖的基本函数。其他3种方法只是帮助器方法,使您编写的代码更少 setImageURI和setImageBitmap都在UI线程上运行。我想说setImageBitmap比第一个快一点。setImageURI实际上取决于Uri资源来自何处,例如,Uri可能指向甚至未存储在手机上的远程文件 setImageURI不适合在UI线程上用作读取和解码,这可能会导致延迟中断 最好使用以下选项:- 改为设置ImageDrawableAndroid.graphics.drawable.drawable或设置ImageBitMapAndroid.graphics.Bitmap和BitmapFactory 您还可以从uri返回位图并在imageview中使用它 有时在imageview上加载大位图可能会导致内存不足异常。因此,您应该高效地加载位图
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));
为了更好地理解,请参见这些链接
Android开发者文档:
ImageView有4个API来指定图像 SetImageDrawable可绘制可绘制 SetImageBitMap位图 setImageResourceint剩余 SetImageUri 这里setImageDrawable是其他API所依赖的基本函数。其他3种方法只是帮助器方法,使您编写的代码更少 setImageURI和setImageBitmap都在UI线程上运行。我想说setImageBitmap比第一个快一点。setImageURI实际上取决于Uri资源来自何处,例如,Uri可能指向甚至未存储在手机上的远程文件 setImageURI不适合在UI线程上用作读取和解码,这可能会导致延迟中断 最好使用以下选项:- 改为设置ImageDrawableAndroid.graphics.drawable.drawable或设置ImageBitMapAndroid.graphics.Bitmap和BitmapFactory 您还可以从uri返回位图并在imageview中使用它 有时在imageview上加载大位图可能会导致内存不足异常。因此,您应该高效地加载位图
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));
为了更好地理解,请参见这些链接
Android开发者文档:
我如何知道reqWidth和reqHeight的值是多少,这样图像就不会看起来模糊?我如何知道reqWidth和reqHeight的值是多少,这样图像就不会看起来模糊?