Reputation: 331
I am trying to save and retrieve a Bitmap from internal storage but everytime I try to load bitmap, BitMapFactory throws Exception:
BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: android.graphics.Bitmap@b35e414: open failed: ENOENT (No such file or directory)
I have tried nearly all solutions given by similar threads on this website, but none worked for me. And this exception is thrown 4 times, though I am reading only one image.How?
And this is the code I am using to save and retrieve images from storage.
public static void saveFile(Context context, Bitmap b, String picName) {
FileOutputStream fos;
try {
fos = context.openFileOutput(picName, Context.MODE_PRIVATE);
b.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (IOException e) {
Log.e("store DRV image", e.getMessage());
e.printStackTrace();
}
}
public static Bitmap loadBitmap(Context context, String picName) {
Bitmap b = null;
FileInputStream fis;
try {
fis = context.openFileInput(picName);
b = BitmapFactory.decodeStream(fis);
fis.close();
} catch (IOException e) {
Log.e("get stored DRV image", e.getMessage());
e.printStackTrace();
}
return b;
}
I got this code from a thread on this website, and all comments were good. But its not working for me. I have added permissions in Manifest.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Lastly, I am using random generated UIDs as filename. The UIDs are generated using Firebase SDK. So the UID may contain numbers or other characters like
XXgKbRiS5ogQz1euqiyRsC1ggBS2
. So is this a wrong way to name a file? and hence exception is thrown?
Upvotes: 1
Views: 543
Reputation: 666
//Make sure permission are granted //For saving
File file=saveFile(contex,bitmap,picName);
//For fetching
File dir = new File(Environment.getExternalStorageDirectory(), picName);
BitmapFactory.decodeFile(file.getPath())
/**
* @param context
* @param b
* @param picName
*/
public static File saveFile(Context context, Bitmap b, String picName) {
FileOutputStream fos;
File dir = new File(Environment.getExternalStorageDirectory(), "My Images");
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, picName);
try {
fos = context.openFileOutput(file.getPath(), Context.MODE_PRIVATE);
b.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (IOException e) {
Log.e("store DRV image", e.getMessage());
e.printStackTrace();
}
return file;
}
Upvotes: 0
Reputation: 1971
You need to add user permission above 6.0:
Add library:
compile 'pub.devrel:easypermissions:0.2.1'
private String[] galleryPermissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (EasyPermissions.hasPermissions(this, galleryPermissions)) {
pickImageFromGallery();
} else {
EasyPermissions.requestPermissions(this, "Access for storage",
101, galleryPermissions);
}
Upvotes: 1
Reputation: 23881
Add this code in onCreate() of your Activity:
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
catch the result in same activity using:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 0: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getContext(), "Permission granted", Toast.LENGTH_SHORT).show();
//call your method
} else {
Toast.makeText(getContext(), "Permission denied", Toast.LENGTH_SHORT).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
Learn more about Runtime Permission from HERE
Upvotes: 1