4

I'm using Gallery3D (froyo-stable)

I'm trying to make a little app that displays only images from a specific folder in a gallery view.

Uri targetUri = Media.EXTERNAL_CONTENT_URI;
String folderPath = Environment.getExternalStorageDirectory().toString() + "/DCIM/";
int folderBucketId = folderPath.toLowerCase().hashCode();
targetUri = targetUri.buildUpon().appendQueryParameter("bucketId", String.valueOf(folderBucketId)).build();

In initializeDataSource()

// Creating the DataSource objects.
final LocalDataSource localDataSource = new LocalDataSource(Gallery.this, targetUri.toString(), false);

But I have error

"Error finding album " + bucketId);

In CacheService.loadMediaSets.:

 Log.e(TAG, "Error finding album " + bucketId);

How can I fix this? Thankyou

Nam Vu
  • 5,669
  • 7
  • 58
  • 90
  • You want the path of a particular folder and retrieve the images from that folder?? – Raghunandan Mar 23 '13 at 08:51
  • yes, display all images from only one folder – Nam Vu Mar 23 '13 at 10:09
  • 1
    i do not know about gallery 3d. I have used 3d carosel using renderscript. But do you need code fro extracting image paths from a single folder assuming that folder has only images. – Raghunandan Mar 23 '13 at 10:56
  • assuming you need to extract path of files under a folder u can use the code below. You can also consider using carousel 3d using renderscript – Raghunandan Mar 24 '13 at 10:38
  • Raghunandan: thanks for your comment, but what i want is something like @Joe's answer – Nam Vu Mar 24 '13 at 12:06

2 Answers2

2

Assuming you need to find path of files under a folder.

    private String[] mFileStrings;
private File[] listFile;

public void getFromSdcard()
{
    File file=  new File(android.os.Environment.getExternalStorageDirectory(),"Your Folder Name");

        if (file.isDirectory())
        {
            listFile = file.listFiles();
            mFileStrings = new String[listFile.length];

            for (int i = 0; i < listFile.length; i++)
            {
                mFileStrings[i] = listFile[i].getAbsolutePath();
                System.out.println("...................................."+mFileStrings[i]);
            }
        }
}

In my phone my internal memory was named sdcard0. So to make sure you get the path of the right one you can use the below code.

 String externalpath = new String();
 String internalpath = new String();

public  void getExternalMounts() {
Runtime runtime = Runtime.getRuntime();
try
{
Process proc = runtime.exec("mount");
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
String line;

BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
    if (line.contains("secure")) continue;
    if (line.contains("asec")) continue;

    if (line.contains("fat")) {//external card
        String columns[] = line.split(" ");
        if (columns != null && columns.length > 1) {
            externalpath = externalpath.concat("*" + columns[1] + "\n");
        }
} 
        else if (line.contains("fuse")) {//internal storage
        String columns[] = line.split(" ");
        if (columns != null && columns.length > 1) {
            internalpath = internalpath.concat(columns[1] + "\n");
        }
    }
}
}
catch(Exception e)
{
    e.printStackTrace();
}
 System.out.println("Path  of sd card external............"+externalpath);
 System.out.println("Path  of internal memory............"+internalpath);
}

An Alternative:

Also you can use 3d carousel to display images using renderscrip as an alternative.

http://code.google.com/p/android-ui-utils/downloads/detail?name=CarouselExample.zip.

Resulting snapshot. Carousel 3d tested on Jelly Bean.

enter image description here

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
1

The "Error finding album" issue is most likely due to the fact that the "DCIM" folder that you get the bucketId from in your code does not have any images directly in it. You should not get the error if for example you use "/DCIM/Camera" instead (assuming there are some images there).

However, if I understand correctly what you want, I believe there are additional modifications that you need to make on the Gallery3D code in order to make it display a specific folder when launched if you follow this route (since the code is just not designed to be used like that).

Instead of using your code above, I believe you can achieve what you want more easily by setting the intent to a specific one in onCreate():

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setIntentToSpecificFolder();

    mApp = new App(Gallery.this);
    // :
    // the rest of onCreate() code
    // :
    Log.i(TAG, "onCreate");
}

private void setIntentToSpecificFolder() {
    String folderPath = Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera";
    int folderBucketId = folderPath.toLowerCase().hashCode();
    Uri targetUri = Media.EXTERNAL_CONTENT_URI.buildUpon().appendQueryParameter("bucketId", String.valueOf(folderBucketId)).build();

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(targetUri, "vnd.android.cursor.dir/image");
    setIntent(intent);
}

Basically what we are doing here is leveraging the ACTION_VIEW intent handling of the app when it is supplied with a vnd.android.cursor.dir/image MIME type.

Joe
  • 14,039
  • 2
  • 39
  • 49
  • Thanks, how can i use getContentUri in API level 8 or something like that – Nam Vu Mar 24 '13 at 09:53
  • Can i use Images.Media.EXTERNAL_CONTENT_URI instead of this? – Nam Vu Mar 24 '13 at 10:23
  • Yes, it should work too. I edited the answer to use that instead. – Joe Mar 24 '13 at 15:34
  • This solution works great for displaying an album when using the Android Gallery app, but it crashes Google's "Photos" app. Is there something that can be done to make it work for both? – Hostile Aug 16 '14 at 19:28