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スティックが追加されたときに通知を受け取るブロードキャストレシーバーが存在し、オンがすでに接続されているかどうかも確認できます)

4

2 に答える 2

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 に答える
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 に答える