Reputation: 2398
I am trying to write a Bitmap into SD Card(as Png file), Find below the Code
File file = new File(getExternalFilesDir(null), "DemoFile.png");
OutputStream os = new FileOutputStream(file);
Log.d("JS", "File name -> " + file.getAbsolutePath());
//File name -> /mnt/sdcard/Android/data/com.pocmodule/files/DemoFile.png
bmp.compress(Bitmap.CompressFormat.PNG, 90, os); //here bmp is of type 'Bitmap'
os.close();
But I do not see the file 'DemoFile.png' being created in SD Card. Let alone the Png File, I do not even see the directory 'com.pocmodule' available in the SD Card.
Am I missing anything in my code?
Upvotes: 1
Views: 236
Reputation: 2398
Thanks @CommonsWare for the help. I finally have the Png file available in SD Card :). For anyone who wants to know the working code, Please find below
File file = new File(getExternalFilesDir(null), "DemoFile.png");
FileOutputStream os = new FileOutputStream(file);
Log.d("JS", "File name -> " + file.getAbsolutePath());
//Here 'bmp' is of type Bitmap
bmp.compress(Bitmap.CompressFormat.PNG, 90, os);
os.flush();
os.getFD().sync();
os.close();
MediaScannerConnection.scanFile(this,
new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
Upvotes: 1
Reputation: 1007494
I mount my Device(with USB debugging mode selected) and explored the directory
That does not explore the filesystem. It explores what is known to MediaStore
.
So, first, call flush()
, then getFD().sync()
, on your FileOutputStream
before you call close()
on it, to ensure that all bytes are written to disk.
Then, use MediaScannerConnection
and scanFile()
to teach MediaStore
about your file.
This may still require you to do some sort of "refresh" in your desktop OS's file explorer window to see the change, or possibly even unplug and re-plug in your device, due to the desktop OS possibly caching "directory" contents.
Upvotes: 2