0

我可以在不启动文档选择器并让用户选择目录的情况下以某种方式找出当前可用的存储吗?

我想显示所有存储根,如下所示:

File mainRoot = Environment.getExternalStorageDirectory();
// any root path that is available => I don't need access rights here, I just want to know which storages are available and "mounted"
DocumentFile sdCardRoot = ???;
DocumentFile usbCardRoot = ???;
DocumentFile gDriveRoot = ???;

为什么?

实际上我想知道在告诉用户他需要通过文档选择器选择 sd 卡根目录之前是否有可用的 sd 卡...

所以我想要以下:

  1. 检查 SD 卡是否可用
  2. 如果没有,什么也不做
  3. 如果 sd 卡可用,告诉用户我的应用需要权限才能读取 sd 卡,并要求用户通过存储访问框架选择 sd 卡根目录

我想对 USB 记忆棒做同样的事情,但我知道该怎么做(存在广播接收器以在添加 USB 记忆棒时收到通知,我也可以检查 on 是否已经连接)

4

2 回答 2

0

You can do the following:

String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {
    //Ask for the permission

    File externalRoot = Environment.getExternalStorageDirectory(); //gives the root directory of External storage
}

Edit:

Go to your .android\avd\"avd_name".avd folder , and open config.ini ; Make sure you have ,

hw.sdCard=yes
sdcard.size=100M
sdcard.path=[Path to ".android" directory]/.android/avd/Nexus_5X_API_23.avd/sdcard.img
于 2016-02-29T14:38:36.880 回答
0

这是我到现在为止的事情:

File root = Environment.getExternalStorageDirectory();
DocumentFile sdCardRoot = null;

File[] fs = context.getExternalFilesDirs(null);
// at index 0 you have the internal storage and at index 1 the real external...
if (fs != null && fs.length >= 2)
{
    String mainPath = fs[0].getAbsolutePath();
    String subPath = mainPath.replace(root.getFolder().getAbsolutePath(), "");
    String pathSDCard = fs[1].getAbsolutePath().replace(subPath, "");
    String relTreePath = pathSDCard.substring(1).replace("storage", "tree") + "%3A";
    // this string is defined com.android.externalstorage.ExternalStorageProvider... but seems to not be accessable in code...
    Uri treeUri = Uri.withAppendedPath(Uri.parse("content://com.android.externalstorage.documents"), relTreePath);
    sdCardRoot = DocumentFile.fromTreeUri(context, treeUri);
}

// now you have the internal storage root in "root" - this path is directly readable via the `File` class
// and the sd card's root is in sdCardRoot and can be managed via the `DocumentFile` class...
于 2016-02-29T18:00:31.837 回答