android - 打算在 Marshmallow 中使用存储资源管理器吗?

标签 android android-intent android-6.0-marshmallow android-external-storage

在 Android 6.0 Marshmallow 中,“设置”→“存储和 USB”→“浏览”下有一个存储资源管理器。

是否可以使用Intent从应用程序内启动此Activity

我发现我可以启动 Settings Activity 与 ACTION_INTERNAL_STORAGE_SETTINGSACTION_MEMORY_CARD_SETTINGS但是我还没有找到启动“探索” Activity 的方法。 我想在探索模式下使用预定义路径启动它,可能通过 Intent 额外或其他方式,例如 intent.putExtra("PATH", getFilesDir() + "/stuff");

Intent intent = new Intent(Settings.ACTION_INTERNAL_STORAGE_SETTINGS);
startActivity(intent);

是否可以启动以下任何一项?

  • com.android.externalstorage
  • com.android.documentsui
  • com.android.documentsui/.DocumentsActivity

是否可以使用预定义路径启动 Activity ?

最佳答案

in Android 6.0, you can use Intent same as previous version of android OS which open default storage explorer with recent files you can also change its to show external storage using option menu there is also navigation drawer for more options like photos,image,videos,Audio filters etc. below is code

public void openFileEx() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");
        startActivityForResult(intent, 15);
    }

但在 android 6.0 Marshmallow 中,您必须请求 android.permission.READ_EXTERNAL_STORAGE 的许可才能从外部存储读取文件,如下所示

if (Build.VERSION.SDK_INT < 23) {
                    openFileEx();
                } else {
                    if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)
                            != PackageManager.PERMISSION_GRANTED) {
                        requestContactsPermissions();
                    } else {
                        openFileEx();
                    }
                }

public void requestContactsPermissions() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.READ_EXTERNAL_STORAGE)) {
            Log.i("PERMISIOM",
                    "Displaying contacts permission rationale to provide additional context.");
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    11);
        } else {
            // Contact permissions have not been granted yet. Request them directly.
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 11);
        }
    }

您还可以使用上面的代码检查更多权限,这是 android-RuntimePermissions 的示例在 Github 上。

关于android - 打算在 Marshmallow 中使用存储资源管理器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35190080/

相关文章:

android随机崩溃并发问题

android - 如何防止双击 ListView?

android - 如何在 Android Studio 中禁用 list 合并

android - 如何取消处于运行状态的AsyncTask。

android - getAllNetworkInfo() 在 M 中已弃用,但其替代品具有不同的行为

java - map 在 OpenStreetMap (osm) 中不可见

android - 尝试启动 Google map 导致我的应用程序崩溃

android - 从命令行打开特定的 Activity

android - 如何使用从您的应用程序外部提供的 "Text Selection"的新 Android M 功能?

android - USE_CREDENTIALS 在新的 Android M API 中不可用