prom85 February 2016

Get storage type of local path

If I have paths like following:

  1. /storage/emulated/0/...
  2. /storage/UsbDriveA/...
  3. /sdcard/...

I basically have following questions:

  • How do I find out which storage they are located on? (USB stick, external storage, internal storage)
  • How do I find out which type they are? (primary storage (I have direct r/w access), secondary storage (on android >=4.4, I don't have direct w access and I need to acquire this right through the Storage Access Framework if I need it))
  • How do I find out what the ROOT path is?

Results that I want

  1. This is: internal storage, primary storage, root path is /storage/emulated/0/
  2. This is: USB Stick, secondary storage, root path is /storage/UsbDriveA/
  3. This is: external storage, secondary storage, root path is /sdcard/

I know the paths may vary from phone to phone, so how do I find out which path is located on which storage and which permissions I do have on which paths? I know I can guess by the names of the path, but I'm interested in a reliable way that works on all (or at least on most) phones...

Answers


adev February 2016

You need to check access for every primary and secondary storage through Environment class. You should do something like this:

Boolean canWrite =  Environment.getExternalStorageDirectory().canWrite(); if(canWrite)// do something with externalstorage

and use Environment.getRootDirectory() for internal root

for USB you need to check it with

public static boolean isConnected(Context context) {
    intent = context.registerReceiver(null, new IntentFilter("android.hardware.usb.action.USB_STATE"));
    return intent.getExtras().getBoolean("connected");
}

Post Status

Asked in February 2016
Viewed 1,343 times
Voted 6
Answered 1 times

Search




Leave an answer