【问题标题】:Get external storage path in android app for tablet在平板电脑的android应用程序中获取外部存储路径
【发布时间】:2019-12-13 09:53:17
【问题描述】:

如何以编程方式在微调器中显示所有外部存储,并仅访问从微调器中选择的外部存储

【问题讨论】:

  • Environment.getExternalStorageDirectory() 获取外部存储路径
  • @keser getExternalStorageDirectory() 已被弃用。

标签: android android-studio android-fragments android-developer-api


【解决方案1】:

获取外部存储路径 Environment.getExternalStorageState()

final String state = Environment.getExternalStorageState();

if ( Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state) ) {  // we can read the External Storage...           
    //Retrieve the External Storage:
    final File primaryExternalStorage = Environment.getExternalStorageDirectory();

    //Retrieve the External Storages from the root directory:
    final String externalStorageRootDir;
    if ( (externalStorageRootDir = primaryExternalStorage.getParent()) == null ) {  
        Log.d(TAG, "External Storage: " + primaryExternalStorage + "\n");
    }
    else {
        final File externalStorageRoot = new File( externalStorageRootDir );
        final File[] files = externalStorageRoot.listFiles();

        for ( final File file: files ) {
            if ( file.isDirectory() && file.canRead() && (file.listFiles().length > 0) ) {  // it is a directory (not a External drive)...
                Log.d(TAG, "External Storage: " + file.getAbsolutePath() + "\n");
            }
        }
    }
}

更多信息试试这个链接 Find location of a removable SD card

【讨论】:

  • getExternalStorageDirectory() 已被弃用。
【解决方案2】:

试试这个库也许对你有帮助。 文件和文件夹选择器

 dependencies {
      compile 'com.github.isabsent:filepicker:1.1.01'
   }

https://github.com/isabsent/FilePicker?utm_source=android-arsenal.com&utm_medium=referral&utm_campaign=6975

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2015-12-18
    相关资源
    最近更新 更多