Reputation: 1889
How can I read an image from the following paths as a bitmap? Thank you.
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";
Upvotes: 0
Views: 1339
Reputation: 10856
other answer is correct but may be you will get a OutOfMemoryError
for high resolution
images like images of camera pictures. so to avoid this you can use below function
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;
}
see this for that error https://stackoverflow.com/a/13226946/942224
Upvotes: 0
Reputation: 14590
Use this method in BitmapFactory it will return you a bitmap object..
BitmapFactory.decodeFile(path);
Upvotes: 1
Reputation: 1570
should be use ==> BitmapFactory.decodeFile(pathName)
method. If file in external stroge declare permission in manifest
Upvotes: 1