I have an application in which an ImageView is set and can be clicked to be opened in the gallery.
By default, I use the following code to get a file directory from the external storage to store my jpegs:
File picsDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"MyCameraApp");
But! Suppose the external storage is not mounted or simply does not exist (Galaxy Nexus), this doesn’t work. So I wrote an if-statement around it and get the internal cache dir as a fall back.
String state = Environment.getExternalStorageState()
if(Environment.MEDIA_MOUNTED.equals(state)){
File picsDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"MyCameraApp");
}else{
context.getCacheDir();
}
The images show up fine in the ImageView, but don’t come through when my intent launches.
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(imgFile), "image/jpeg");
startActivity(intent);
The gallery gets loaded, but shows a black screen. Presumably because the gallery has no access to files in the cache dir of my app.
As an alternative, I tried using the media content provider that uses MediaStore.Images.Media.INTERNAL_CONTENT_URI, but this leads to an error when trying to inser the image:
java.lang.UnsupportedOperationException: Writing to internal storage is not supported.
What should I do?
i suppose the problem here is that you are trying open with the gallery a file saved in a private space of memory (getCacheDir return a path relative to your application and only your application can access that memory path)
If you can’t save in external memory, you can try to save in a public path (but that way your media files can be manipulated by every app and if you uninstall your application it doesn’t clean generated media that you saved there)
If you want to use private internal memory, you can write your ContentProvider
i edit to post a content provider i use to acomplish what i said.
this is my content provider (i just posted the relevant part you need):
then you need in the manifest the reference to your contentprovider, in my case it was
and then use it like this
in my case m is an entity that store an id that point to a sqlite db and i use a class that fetch data to populate again the object (with _mediaData), you can just change the code to fit your needs
this way i solved exactly your problem in my application