大家好,我知道在Android R我无法打开文件夹/storage/emulated/0/Android/data/section,所以我创建了一个文件夹/storage/emulated/0/Document/myapp来保存来自我的应用程序的图像(截图),但我尝试了以下方法仍然无法打开此文件夹:
fun openDirectory() {
val dir: File
// Choose a directory using the system's file picker.
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
if (Build.VERSION_CODES.R > SDK_INT) {
dir = File(
Environment.getExternalStorageDirectory().path
+ "//CHAN"
)
} else {
dir = File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)
.path
+ "//CHAN"
)
}
val pickerInitialUri = Uri.parse(dir.toString())
putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)
}
startActivityForResult(intent, YOUR_REQUEST_CODE)
}
fun openFile() {
val dir: File
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "file/*"
if (Build.VERSION_CODES.R > SDK_INT) {
dir = File(
Environment.getExternalStorageDirectory().path
+ "//CHAN"
)
} else {
dir = File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)
.path
+ "//CHAN"
)
}
val pickerInitialUri = Uri.parse(dir.toString())
putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)
}
startActivityForResult(intent, YOUR_REQUEST_CODE)
}
open fun openFolder(): Unit {
val location : String = "/storage/emulated/0/Documents/CHAN/"
val intent = Intent(Intent.ACTION_VIEW)
val mydir = Uri.parse("$location")
intent.setDataAndType(mydir, "application/*") // or use */*
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent)
}
我用代码创建文件夹:
final File dir;
if (Build.VERSION_CODES.R > Build.VERSION.SDK_INT) {
dir = new File(Environment.getExternalStorageDirectory().getPath()
+ "//CHAN");
} else {
dir = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_DOCUMENTS).getPath()
+ "//CHAN");
}
我的应用程序:minSdkVersion 29
,targetSdkVersion 30
我有权限WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE但是我没有权限MANAGE_EXTERNAL_STORAGE有没有办法自动打开文件夹?或者有没有更好的方法来保存和查看这些照片?谢谢。
2条答案
按热度按时间2ic8powd1#
您是否在Your_Request_Code为42的位置输入了Intent?这似乎是其他人正在使用的Intent。这也需要位于Activity Result上
rjzwgtxy2#
EXTRA_INITIAL_URI应使用文档ID指定文档URI或树URI。
示例: