I'm writing an android application that takes some pictures and would like to save them all in a unique directory associated to my application.
This directory should be accessible from the standard Gallery, in such a way that the user can later (when application is not necessarily running) check the pictures that were taken.
My problem is that every different phone vendor, with a different android version, has different paths for gallery.As an example:
Environment.getExternalStorageDirectory() + Environment.DIRECTORY_PICTURES +"/myFolder"
will work on Samsung Galaxy Nexus
running android 4.1.1
, and on Asus Transformer Pad
running android 4.0.3
, but not on HTC Desire
running android 2.3.5
.
This will cause my application to crash when trying to save a new directory within the specified path, as stated below:
boolean success = false;
myFolder = new File( Environment.getExternalStorageDirectory() + Environment.DIRECTORY_PICTURES + "/myFolder" );
if( myFolder.exists() ){
//do nothing
}else{
success = dvaFolder.mkdir();
if( success ){
// Do something on success
/*
* folder has been created
*/
} else {
// Do something else on failure
/*
* folder creation failed
*/
throw new RuntimeException("File Error in writing new folder");
}
}
How can I write a directory that will be accessible in gallery for all different vendors and android versions?
NOTE:
Logcat isn't much useful, cause it just returns the run time Exception.
You should check that you have included the "WRITE_EXTERNAL_STORAGE" permission in your application Manifest, it could give such errors. Also, have you checked that the path does not simply look invalid by printing it?
I've had the same issue with the bluetooth folder, I created a two methods to search for the bluetooth folder on the phone, by traversing the whole file structure on the phone. The code I wrote is beneath, it is a bit dirty, but it works quite well in my case
Use the method above in this method:
By saying that the code is dirty, I mean that I could wrote it more compact and another issue by doing it this way: if there are two folders with the name bluetooth, I'm stuck..
I hope I understood your question right.
Here is working code for taking picture and saving to external storage and another method for saving to 'files' directory on device. In my case I needed only one image (something.png) which will be always refreshed after user takes new picture.
Action when user clicks on button for camera:
Action which is performed on activity result:
Bonus method for saving image to device in files directory if someone needs: