I have a little issue with creating a directory and saving a file to it on my android application. I'm using this piece of code to do this :
String filename = "MyApp/MediaTag/MediaTag-"+objectId+".png";
File file = new File(Environment.getExternalStorageDirectory(), filename);
FileOutputStream fos;
fos = new FileOutputStream(file);
fos.write(mediaTagBuffer);
fos.flush();
fos.close();
But it's throwing an exception :
java.io.FileNotFoundException: /mnt/sdcard/MyApp/MediaCard/MediaCard-0.png (No such file or directory)
on that line : fos = new FileOutputStream(file);
If I set the filename to : "MyApp/MediaTag-"+objectId+"
it's working, but If I try to create and save the file to an another directory it's throwing the exception. So any ideas what I'm doing wrong?
And another question: Is there any way to make my files private in external storage so user can't see them in gallery, only if he connect his device as Disk Drive
?
since android 4.4 file saving has been changed. there is
it retuns an array.
when name is null
the first value is like /storage/emulated/0/Android/com.my.package/files
the second value is like /storage/extSdCard/Android/com.my.package/files
android 4.3 and less it retuns a single item array
parts of little messy code but it demonstrates how it works:
Try This :
I have created an AsyncTask for saving bitmaps.
This code is Working great & Worked on KitKat as well. Appreciate @RajaReddy PolamReddy
Added few more steps here and also Visible on Gallery as well.
This is Media scanner to Visible in Gallery.
Use this function to save your bitmap in SD card
and add this in manifest
EDIT: By using this line you will be able to see saved images in the gallery view.
look at this link also http://rajareddypolam.wordpress.com/?p=3&preview=true
Probably exception is thrown because there is no
MediaCard
subdir. You should check if all dirs in the path exist.About visibility of your files: if you put file named
.nomedia
in your dir you are telling Android that you don't want it to scan it for media files and they will not appear in the gallery.