I'm trying to save a bitmap into the Pictures directory. Here's the code
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File file = new File(path, "test1.PNG");
try {
path.mkdirs();
OutputStream out = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
Log.w("ExternalStorage", "Error writing " + file, e);
}
But the execution got stuck at OutputStream out = new FileOutputStream(file);
I used the debugger and the full path returns mnt/sdcard/Pictures/test1.PNG
, is mnt/
the culprit why I couldn't get past OutputStream out = new FileOutputStream(file);
? Because I can only see sdcard/
in my file directory.
thanks!
/sdcard
is a softlink to /mnt/sdcard
...
and /sdcard
is read only in file system so better use /mnt/sdcard/
..
You can get and access the sdcard directory using this Environment.getExternalStorageDirectory()
as the mnt/sdcard or sdcard/ its an device dependent directory that how OS was access and use the external directory no need to worry for different device and different directory was return by this method.
EDIT
For accessing external storage need permission and define in androidmanifest.xml
file as user permission
WRITE_EXTERNAL_STORAGE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With