如何从以下路径读取图像作为位图?谢谢你。
String path = "file:///storage/emulated/0/Pictures/MY_FILES/1371983157229.jpg";
String path = "file:///storage/sdcard0/Android/data/com.dropbox.android/files/scratch/Camera%20Uploads/2045.12.png";
应该使用 ==>BitmapFactory.decodeFile(pathName)
方法。如果外部存储中的文件在清单中声明权限
在 BitmapFactory 中使用此方法,它将返回一个位图对象..
BitmapFactory.decodeFile(path);
其他答案是正确的,但可能你会得到一个图像OutOfMemoryError
,high resolution
比如相机图片的图像。所以为避免这种情况,您可以使用以下功能
public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//The new size we want to scale to
final int REQUIRED_WIDTH=WIDTH;
final int REQUIRED_HIGHT=HIGHT;
//Find the correct scale value. It should be the power of 2.
int scale=1;
while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
scale*=2;
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
}
catch (FileNotFoundException e) {}
return null;
}