I want to show an images gallery. The problem is during the loading of them.
At the beginning, I was loading the images with :
images = new File("/sdcard/DCIM/Camera/");
if(images.listFiles().length==0)
//no images, do some other stuff...
else
//put images in the gallery and do some stuff
This code works with the emulator (Android 2.2 – API Level 8) and with a Samsung Galaxy S2 (Android 2.2.1). But I ask some friends to test this code with their own phones (HTC, LG…).
With an HTC Desire, there is a problem during this loading. The code is going into “//no images” but they have images in the album (for example from Camera…) My friend said to me that :
album is stored in the internal storage and not in the SD Card with HTC.
So i’ve tried to check others directory, like this :
//Get the internal content
images = new File(MediaStore.Images.Media.INTERNAL_CONTENT_URI.getPath());
if (!images.isDirectory()) {
//Get the external content
images = new File(MediaStore.Images.Media.EXTERNAL_CONTENT_URI.getPath());
}
if (!images.isDirectory()) {
//Get the SD Card content
images = new File("/sdcard/DCIM/Camera/");
}
But still not working 🙁 That is not the good directory :/
So, my question is : how can i get the good path for images with all android phone ? is their an Object which is able to return the path of the album ?
Thanks 🙂
(sorry for mistakes)
I did some checking and it looks like you are trying to access the MediaStore incorrectly. It should be accessed via a cursor and not directly as a file.
This is because as mentioned at http://developer.android.com/guide/topics/providers/content-providers.html
So basically you can’t read the URI as a file. It is a table of data.
Here’s an example from that same page on how to access it.
Also, here’s another Stackoverflow question that has some examples that shows how to use the cursor.
How to query Android MediaStore Content Provider, avoiding orphaned images?
Finally, based on the links above I would just use the mediastore object as a cursor on all devices and that will solve the problem you are having with different directories.
Hope this helps,
George