0

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)

Machavity
  • 30,841
  • 27
  • 92
  • 100
  • In my HTC Ddesire photos from standard camera application stored in: `/sdcard/DCIM/100MEDIA/`, photos from Camera360 app in `/sdcard/DCIM/Camera/`. – jamapag Jul 18 '11 at 12:31

1 Answers1

1

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

Content providers expose their data as a simple table on a database model, where each row is a record and each column is data of a particular type and meaning.

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.

import android.provider.Contacts.People;
import android.content.ContentUris;
import android.net.Uri;
import android.database.Cursor;

// Use the ContentUris method to produce the base URI for the contact with _ID == 23.
Uri myPerson = ContentUris.withAppendedId(People.CONTENT_URI, 23);

// Alternatively, use the Uri method to produce the base URI.
// It takes a string rather than an integer.
Uri myPerson = Uri.withAppendedPath(People.CONTENT_URI, "23");

// Then query for this specific record:
Cursor cur = managedQuery(myPerson, null, null, null, null);

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

Community
  • 1
  • 1
George Baker
  • 1,207
  • 9
  • 13