[英]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)
當我啟動 te 應用程序時,目錄選擇器會彈出,我可以選擇一個目錄。
在 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)
}
我嘗試過的事情:
uri.toString()
而不是uri.path
flags = Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
盡管我盡了最大的努力,但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)
}
它正在工作,看起來我可以從這里繼續前進。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.