无法打开指定的文件夹android R

vdzxcuhz  于 2023-01-11  发布在  Android
关注(0)|答案(2)|浏览(321)

大家好,我知道在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 29targetSdkVersion 30我有权限WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE但是我没有权限MANAGE_EXTERNAL_STORAGE有没有办法自动打开文件夹?或者有没有更好的方法来保存和查看这些照片?谢谢。

2ic8powd

2ic8powd1#

您是否在Your_Request_Code为42的位置输入了Intent?这似乎是其他人正在使用的Intent。这也需要位于Activity Result上

if (_requestCode == 42 && _resultCode ==
Activity.RESULT_OK) {
Uri uri = null;
if (_data != null) {
uri = _data.getData();
ur = uri.toString();
}
}
rjzwgtxy

rjzwgtxy2#

EXTRA_INITIAL_URI应使用文档ID指定文档URI或树URI。
示例:

// Directories : Pictures , Music , Movies

val documentUri= Uri.parse("content://com.android.externalstorage.documents/document/primary%3APictures%2FmyApp%2F")
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
        intent.type = "*/*"
        intent.addCategory(Intent.CATEGORY_OPENABLE)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            intent.putExtra(
                DocumentsContract.EXTRA_INITIAL_URI,
                documentUri
            )
        }

startActivityForResult(intent, YOUR_REQUEST_CODE)

相关问题