-2

I want access to a file: "/mnt/media_rw/72Ab-289A/myFile.mp3" This file is located on my USB Stick on my Moto G5 Android 7 Smartphone.

But it's not possible... I can't read the /mnt/media_rw/ folder.

The permissions: WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE are set, so this is not the problem.

I've tried it for 5 hours, with google, Environment.getExternalStorageDirectory(), ContextCompat.getExternalFilesDirs, File.readFiles, and much more... nothing is working.

Is there anyone out there and can help me, with a complete solution?

Kyle
  • 21
  • 6
  • Possible duplicate of [Get access to USB mass storage device in android](https://stackoverflow.com/questions/24075352/get-access-to-usb-mass-storage-device-in-android) – Digvijay Singh Jun 08 '17 at 04:32
  • Thanks, I've read that post a couple of hours before, but is not working. USB stick is not at "/storage/UsbDriveA/" – Kyle Jun 08 '17 at 04:45

3 Answers3

1

It seems that it's something with the USB or Drive Permissions. On my ODROID C2 with Android 6 it's working fine. But with my Moto G5 (Android 7) and Asus Zen Tablet (Android 6) it's not working.

I can select a file with "Intent.ACTION_OPEN_DOCUMENT" File Picker, and on all devices the Document Path is the same... but, some of the Volumes are not there:

Asus Zen (Android 6)

Volume: /storage/emulated/0

selected file: /storage/7C23-16EA/m_BlueforLabrusca.mp3

. . .

ODroid C2 (Android 6)

Volume: /storage/emulated/0

Volume: /storage/internal

Volume: /storage/7C23-16EA

selected file: /storage/7C23-16EA/m_BlueforLabrusca.mp3

StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
Class<?> StorageVolume = Class.forName("android.os.storage.StorageVolume");
Method getVolumeListMethod = StorageManager.class.getDeclaredMethod("getVolumeList");
Method methodGetPath = StorageVolume.getDeclaredMethod("getPath");

Object[] storageVolumeList = (Object[]) getVolumeListMethod.invoke(storageManager);
    final int length = Array.getLength(storageVolumeList);
            for (int i = 0; i < length; i++) {

                Object storageVolumeElement = Array.get(storageVolumeList, i);
                String path = (String) methodGetPath.invoke(storageVolumeElement);
                path = new File(path).getAbsolutePath();

                volumes.add(new File(path));

                Log.i("!!!","Volume: " + path);
            }
Kyle
  • 21
  • 6
  • This gave me more results than all the other methods I've used before. But still wasn't able to detect the flash drive I wanted to see. – rmanalo Mar 30 '19 at 09:14
0

Ok, found it out.

It's not working with the normal File/Storage commands.

The only way is to use the Documents Framework: https://developer.android.com/guide/topics/providers/document-provider.html

Kyle
  • 21
  • 6
0

Try using Storage Access Framework(SAF).

devpelux
  • 2,492
  • 3
  • 18
  • 38