在 Android 6.0 Marshmallow 中,“设置”→“存储和 USB”→“浏览”下有一个存储资源管理器。
是否可以使用Intent
从应用程序内启动此Activity
?
我发现我可以启动 Settings Activity 与 ACTION_INTERNAL_STORAGE_SETTINGS
或ACTION_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/