Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get path of DCIM folder on both primary and secondary storage

Tags:

android

I am writing an application that should upload pictures taken by the camera which are stored in the DCIM/Camera folder on both internal memory and SD card.

This means that before every upload all available storages have to be checked for presence of any images.

I use Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) to access the primary storage, which can be either SD card or internal memory (depends on device).

From documentation:

Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

My question is, how can I access the secondary storage to check for presence of images in the DCIM/Camera folder without hard-coding the path, which does not work well since the SD card might be emulated in different path.

like image 264
Adam Avatar asked Apr 11 '15 09:04

Adam


3 Answers

Try this code for choosing the last used DCIM/Camera folder.

String getDCIMCamera() {
    try {
        String[] projection = new String[]{
                MediaStore.Images.ImageColumns._ID,
                MediaStore.Images.ImageColumns.DATA,
                MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
                MediaStore.Images.ImageColumns.DATE_TAKEN,
                MediaStore.Images.ImageColumns.MIME_TYPE};

        Cursor cursor = getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection,
                null,
                null,
                MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
        if (cursor != null) {
            cursor.moveToFirst();
            do {
                String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                if (path.contains("/DCIM/")) {
                    File file = new File(path);
                    path = file.getParent();
                    cursor.close();
                    return path;
                }
            } while (cursor.moveToNext());
            cursor.close();
        }
    }
    catch (Exception e) {
    }
    return "";
}

See also @Pedram tips: https://stackoverflow.com/a/24189813/966789

like image 126
Alexander Lubyagin Avatar answered Sep 18 '22 11:09

Alexander Lubyagin


Try

File[] aDirArray = ContextCompat.getExternalFilesDirs(context, null);

See http://developer.android.com/reference/android/support/v4/content/ContextCompat.html#getExternalFilesDirs(android.content.Context, java.lang.String)

Remember that external storage (SD Card) is not emulated and all paths are visible to all users.

Return Value

  • null - In case of failure
  • Root path (File) of each mounted external storage.

So if aDirArray.length > 1, then the following gets you the DCIM path you are looking for on the removable storage.

File aExtDcimDir = new File(aDirArray[1], Environment.DIRECTORY_DCIM);

Maybe in your case, you want to check aDirArray[0], aDirArray[1], ... (all Files returned in the array) for the presence of Environment.DIRECTORY_DCIM.

like image 20
Dave Truby Avatar answered Sep 21 '22 11:09

Dave Truby


getExternalStorageDirectory retrieves only from internal storage data but not secondary storage images

This is the way we are getting secondary storage images from android or any folder accessing from secondary storage this is the code......

String secStore = System.getenv("SECONDARY_STORAGE");
File file = new File(secStore + "/DCIM");
File[] listFiles = file.listFiles();
like image 38
sivabrahma Avatar answered Sep 20 '22 11:09

sivabrahma