如何從下列路徑讀取圖像作爲位圖?謝謝。從Android中的自定義應用程序圖像路徑讀取圖像
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";
如何從下列路徑讀取圖像作爲位圖?謝謝。從Android中的自定義應用程序圖像路徑讀取圖像
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)
方法。如果文件在外部stroge聲明許可清單中
使用此方法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;
}
感謝鸚鵡,即工作。 – Nizzy