当我只有一个目录的 uri 时,如何创建文件? (该目录的持久权限由 Android 用户授予。)

How do I create files when all I have is an uri of a directory? (Persistable permission for the directory was granted by the Android user.)

当 android 设备通过 USB 连接到 PC 时,我正在尝试写入和读取也可以从 PC 看到和编辑的文件。这些文件可能是通过 PC 连接创建的。

为了实现这一点,我正在尝试获取一个目录的持久 URI 权限,然后在该目录中创建或读取文件。

我正处于“概念验证”阶段。

在 MainActivity OnCreate 方法中,我有以下代码:

startActivityForResult(Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
    flags = Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
}, GET_PERSISTABLE_PERMISSION)

当我启动应用程序时,目录选择器会弹出,我可以 select 一个目录。

在MainActivity中我还有:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (requestCode == GET_PERSISTABLE_PERMISSION && resultCode == Activity.RESULT_OK) {
        val uri = data?.data!!
        val contentResolver = applicationContext.contentResolver
        val takeFlags: Int = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
        contentResolver.takePersistableUriPermission(uri, takeFlags)

        val tmpFile = File(uri.path, "debugTestFile.txt")
        tmpFile.createNewFile()
        tmpFile.appendText("árvíztűrő tükörfúrógép\r\n")
    }
    super.onActivityResult(requestCode, resultCode, data)
}

我尝试过的事情:

尽管我尽了最大努力,tmpFile.createNewFile() 行还是抛出了一个异常,显示“没有这样的文件或目录”消息。

    Process: hu.ibcs.android.scan, PID: 17309
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.externalstorage.documents/tree/primary:Music/GVtest flg=0xc3 }} to activity {hu.ibcs.android.scan/hu.ibcs.android.scan.MainActivity}: java.io.IOException: No such file or directory
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4846)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4887)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2017)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7397)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
     Caused by: java.io.IOException: No such file or directory
        at java.io.UnixFileSystem.createFileExclusively0(Native Method)
        at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:317)
        at java.io.File.createNewFile(File.java:1008)
        at hu.ibcs.android.scan.MainActivity.onActivityResult(MainActivity.kt:106)
        at android.app.Activity.dispatchActivityResult(Activity.java:8135)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4839)

非常感谢@blackapps 的建议!

我将 MainActivity.onCreate 中的代码更改为:

startActivityForResult(Intent(Intent.ACTION_OPEN_DOCUMENT_TREE), GET_PERSISTABLE_PERMISSION)

MainActivity中的代码为:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (requestCode == GET_PERSISTABLE_PERMISSION && resultCode == Activity.RESULT_OK) {
        val uri = data?.data!!
        val contentResolver = applicationContext.contentResolver
        val takeFlags: Int = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
        contentResolver.takePersistableUriPermission(uri, takeFlags)

        val pickedDir = DocumentFile.fromTreeUri(this, uri)
        val tmpFile = pickedDir.createFile("text/csv", "debugTestFile")
        val out: OutputStream? = getContentResolver().openOutputStream(tmpFile!!.uri)
        out?.write(("\uFEFF" + "árvíztűrő tükörfúrógép\r\n").toByteArray()) // adding BOM to the start for Excel to recognize utf8
        out?.close()
    }
    super.onActivityResult(requestCode, resultCode, data)
}

它正在运行,看起来我可以从这里继续前进。