-7

When I plug my device into big computer I see the following picture

enter image description here

How to find these (two) directories programmatically from withing Android application?

UPDATE

I wrote utility class to deduce roots. Unfortunately, it works for minSdkVersion=19

public class RootsUtil {

    private final static String seed = Environment.DIRECTORY_PICTURES;

    public final static File[] getRoots(Context context) {

        File[] paths = context.getExternalFilesDirs(seed);

        if( paths.length <= 1 ) {
            return new File[] { Environment.getExternalStorageDirectory() };
        }
        else {
            while(true) {
                int count = 1;

                for (int i = 1; i < paths.length; ++i) {
                    if (paths[0].getName().equals(paths[i].getName())) {
                        count++;
                    }
                }
                if( count==paths.length ) {
                    for (int i = 0; i < paths.length; ++i) {
                        paths[i] = paths[i].getParentFile();
                    }
                }
                else {
                    break;
                }
            }
            return paths;
        }
    }
}

The question persists: are there any solutions for at least SDK=15?

P.S.

People downvoting this (absolutely normal) question: you are just declaring yourselves a clowns.

Dims
  • 47,675
  • 117
  • 331
  • 600

2 Answers2

1

How to find these (two) directories programmatically from withing Android application?

You don't.

The one labeled "Phone" presumably is what the Android SDK refers to as external storage. I say "presumably" because device manufacturers seem to change this label — I usually see it called "Internal" or "Internal storage". To get the root of external storage, use Environment.getExternalStorageDirectory(). Note that this requires that you hold the READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permissions, which includes asking for those permissions at runtime.

The one labeled "Card" presumably is referring to some removable media. You cannot work with the root directory of removable storage.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • My `Camera` application successfully stores photos on that removable storage. I wan't too. Am I deprived? – Dims Feb 04 '17 at 14:39
  • 1
    @Dims: You are welcome to use the techniques outlined in [the blog post that I linked to](https://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html) to work with removable storage (e.g., `getExternalFilesDirs()`, `ACTION_OPEN_DOCUMENT`). See also [this SO answer](https://stackoverflow.com/a/40564472/115145). – CommonsWare Feb 04 '17 at 14:43
  • can you hint please, how to find `DCIM` directory with this techniques? – Dims Feb 04 '17 at 14:46
  • @Dims: If you mean "a `DCIM` directory unique for the app", use either `getExternalFilesDirs(Environment.DIRECTORY_DCIM)` or `getExternalMediaDirs(Environment.DIRECTORY_DCIM)`. If those return 2+ items, the second and subsequent ones are on removable storage, are unique for your app, and can be used by you using normal filesystem APIs. If you mean some common `DCIM` directory, on Android 7.0+, [use `createAccessIntent()`](https://developer.android.com/reference/android/os/storage/StorageVolume.html#createAccessIntent(java.lang.String)). – CommonsWare Feb 04 '17 at 14:50
  • @Dims: Or, for a common `DCIM` directory, [use `ACTION_OPEN_DOCUMENT_TREE`](https://developer.android.com/reference/android/content/Intent.html#ACTION_OPEN_DOCUMENT_TREE) on Android 5.0+, though the user might not choose `DCIM` and might not choose removable storage. – CommonsWare Feb 04 '17 at 14:51
  • I need to do this without user interaction. Is this possible? – Dims Feb 04 '17 at 14:56
  • @Dims: You do not have access to a `DCIM` directory on removable storage that belongs to another app, except by involving the user via `ACTION_OPEN_DOCUMENT` or `ACTION_OPEN_DOCUMENT_TREE`. – CommonsWare Feb 04 '17 at 15:29
  • Why? Suppose I anm writining my own Camera app, why it should put it's photos into `/Android/Data/bla.bla.bla/DCIM`, while normal camera app can put into just `/DCIM`? Is this honest competition? – Dims Feb 04 '17 at 15:35
  • @Dims: If you mean `DCIM/` on external storage, you are welcome to use that (`Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)`). If you mean `DCIM/` on removable storage, either the camera app asked you for for permission via one of the above means, or the camera app was pre-installed and received permission by the manufacturer. – CommonsWare Feb 04 '17 at 15:38
  • I wan't to resembe `Camera` and many other applications behavior. Some of them have just setting, where to store data, with selection either device, or SD card. Many other progams can be moved from internal memory to sd card and back by a button. Such applications apparently just don't know where they are. – Dims Feb 04 '17 at 16:03
  • Looks like `ACTION_OPEN_DOCUMENT_TREE` method does not help. See my another question: http://stackoverflow.com/questions/42040984/how-gain-write-access-to-dcim-directory-on-removable-media-in-android – Dims Feb 04 '17 at 16:43
-1

Maybe this will help you:

https://stackoverflow.com/a/40123073/5002496

thanks to that method you can list all mounted external storages paths. I am using it in my project to store data in sd-card and tested it on more than 20 devices.

Community
  • 1
  • 1
RadekJ
  • 2,835
  • 1
  • 19
  • 25