alexrintt / shared-storage

Flutter plugin to work with Android external storage.
http://alexrintt.io/shared-storage/
MIT License
53 stars 24 forks source link

Cannot creat DocumentFile from uri returned by getMediaStoreContentDirectory #105

Closed Bersh closed 1 year ago

Bersh commented 1 year ago

Describe the bug I try to create an instance of DocumentFile from URI returned by getMediaStoreContentDirectory() function. Unfortinately when I call .toDocumentFile() on the URI I get an exception:

E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713): Failed to handle method call
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713): java.lang.IllegalArgumentException: Invalid URI: /external/downloads
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at android.provider.DocumentsContract.getDocumentId(DocumentsContract.java:1278)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at io.lakscastro.sharedstorage.storageaccessframework.lib.DocumentCommonKt.createDocumentFileMap(DocumentCommon.kt:48)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at io.lakscastro.sharedstorage.storageaccessframework.DocumentFileApi.onMethodCall(DocumentFileApi.kt:94)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$DartMessenger(DartMessenger.java:319)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at io.flutter.embedding.engine.dart.-$$Lambda$DartMessenger$TsixYUB5E6FpKhMtCSQVHKE89gQ.run(Unknown Source:12)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at android.os.Handler.handleCallback(Handler.java:938)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at android.os.Looper.loopOnce(Looper.java:201)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at android.os.Looper.loop(Looper.java:288)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at android.app.ActivityThread.main(ActivityThread.java:7839)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

To Reproduce Lets take Downloads folder for example. The same issue happens when you use other media store directories as well. Try to create DocumentFile from Downloads directory URI returned by getMediaStoreContentDirectory call like this.

(await getMediaStoreContentDirectory(MediaStoreCollection.downloads))!.toDocumentFile();

Observe the exception

E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713): Failed to handle method call
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713): java.lang.IllegalArgumentException: Invalid URI: /external/downloads
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at android.provider.DocumentsContract.getDocumentId(DocumentsContract.java:1278)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at io.lakscastro.sharedstorage.storageaccessframework.lib.DocumentCommonKt.createDocumentFileMap(DocumentCommon.kt:48)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at io.lakscastro.sharedstorage.storageaccessframework.DocumentFileApi.onMethodCall(DocumentFileApi.kt:94)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$DartMessenger(DartMessenger.java:319)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at io.flutter.embedding.engine.dart.-$$Lambda$DartMessenger$TsixYUB5E6FpKhMtCSQVHKE89gQ.run(Unknown Source:12)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at android.os.Handler.handleCallback(Handler.java:938)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at android.os.Looper.loopOnce(Looper.java:201)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at android.os.Looper.loop(Looper.java:288)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at android.app.ActivityThread.main(ActivityThread.java:7839)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
E/MethodChannel#io.lakscastro.plugins/sharedstorage/documentfile(13713):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

Expected behavior DocumentFile instance is created from the given URI

alexrintt commented 1 year ago

First off, thanks for the detailed report.

Guess probably you would like to use openDocumentTree instead of getMediaStoreContentDirectory.

getMediaStoreContentDirectory will be removed soon and will be internally integrated.

Let me know if it solves or may I should know something more about your use-case.