3

** EDIT

Rather than an sdcard, i'm accessing it via adapters through the usb port, so it might be better to consider it as an usb drive.

After a lot of struggle and research, i was able to find a way to reach the path to the sdcard (USB drive) i was working with. In general, the path is /mnt/media_rw/XXXX-XXXX (where XXXX-XXXX is a code of the sdcard (USB drive)).

The thing is, i'm trying to read some files that are in there, but when i call

new File("/mnt/media_rw/XXXX-XXXX").listFiles();

i get that returns a null array. I'm pretty certain that has files inside it, since i have a phone where this works, but in that phone, the path it's not "mnt/media_rw/XXXX-XXXX". Using the same path as in that phone does not work.

I've reached that path through this post How can I get the list of mounted external storage of android device

I have 2 permissions: READ_EXTERNAL_STORAGE and READ_PHONE_STATE. I had to add them bot in the manifest and programmatically (don't know why only works when add them in both, but not separated).

** ADDED

public static String findFileInStorages(String filename) throws Exception {
    List<StorageUtils.StorageInfo> storageList = StorageUtils.getStorageList(); // Here i get 2 paths: 1) /storage/emulated/0; 2) /mnt/media_rw/XXXX-XXXX
    for(int i = 0; i < storageList.size(); i++) {
        String pathToFile = findFileWithBasePath(new File(storageList.get(i).path, filename);
        if (pathToFile != null) return pathToFile;
    }
    throw new Exception("Couldn't find file");
}

public static String findFileWithBasePath(File file, String fileName) throws Exception {
    File listFile[] = file.listFiles();
    String outputFile = null;
    if (listFile != null && listFile.length > 0) {
        for (int i = 0; i < listFile.length; i++) {
            if (listFile[i].isDirectory()) {
                outputFile = findFileWithBasePath(listFile[i], fileName);
            } else {
                if(listFile[i].getName().contains(fileName)) {
                    return listFile[i].getAbsolutePath();
                }
            }
            if (outputFile != null) return outputFile;
        }
    }
    return null;
}

The "getStorageList()" method and "StorageUtils" and "StorangeInfo" classes are in the link above. i didn't change a thing of the code in it.

If there is anything i can add, just ask it.

Thanks

Silkking
  • 250
  • 3
  • 15
  • Post your code..... – Vishal Vaishnav Aug 08 '17 at 13:19
  • You do not have filesystem access to arbitrary locations on [removable media](https://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html) on Android 4.4+. – CommonsWare Aug 08 '17 at 13:45
  • Do you think this will help you? https://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory(java.lang.String) – ben_joseph Aug 08 '17 at 13:47
  • @CommonsWare, how can i have filesystem access to those locations? I'm searching for android permission that allows me that – Silkking Aug 08 '17 at 16:43
  • "how can i have filesystem access to those locations?" -- root the device. Otherwise, you can't. Or, stop working with the filesystem, and use the Storage Access Framework instead. – CommonsWare Aug 08 '17 at 16:52

1 Answers1

1

There is a context method to retrieve the path to all sd cards

context.getExternalFilesDirs(null)

Returns absolute paths to application-specific directories on all shared/external storage devices where the application can place persistent files it owns.

Kirill Kulakov
  • 10,035
  • 9
  • 50
  • 67
  • I think i tried this unsuccesfully. The thing is the file i want to read is not from the app, but it's in an external sdcard. With this i tried to look for the file in the folders appear here or it's parents – Silkking Aug 08 '17 at 14:02