Here is what I am trying to achieve. In my app there is an option for user to select a folder and save it so that a service will copy files into it in the background whenever a certain condition is met. So basically user selects the folder once and forgets it.
This folder can be in external SD card and so I cannot use the usual File.DirRootExternal to get the path and perform copy. Thanks to code shared in the community I was able to do it using using intent ACTION_OPEN_DOCUMENT_TREE.
Problem is it returns a URI and not a physical path like /storage/emulated/0/ etc... While I can perform copy options successfully with URI and streams (File.Copy2), I still need to display the full physical path to the folder user selected so that he can make sure the path is right. And URI path like content://com.android.externalstorage.documents/ won't make sense to him. It has to be in the format /storage/emulated/0/.
I tried the code posted here but it is crashing when testing on Android 7.0. Tested uri was (HierarchicalUri) content://com.android.externalstorage.documents/tree/primary%3ADCIM. URI is that of a folder on external SD card.
Check the lines below
The error comes at second line and this is because Cursor1 is not initiliazed as cr.Query from line above does not return anything.
Point to note is I am trying to get path from URI of a folder and not file. Not sure if that is what is causing the error.
This folder can be in external SD card and so I cannot use the usual File.DirRootExternal to get the path and perform copy. Thanks to code shared in the community I was able to do it using using intent ACTION_OPEN_DOCUMENT_TREE.
Problem is it returns a URI and not a physical path like /storage/emulated/0/ etc... While I can perform copy options successfully with URI and streams (File.Copy2), I still need to display the full physical path to the folder user selected so that he can make sure the path is right. And URI path like content://com.android.externalstorage.documents/ won't make sense to him. It has to be in the format /storage/emulated/0/.
I tried the code posted here but it is crashing when testing on Android 7.0. Tested uri was (HierarchicalUri) content://com.android.externalstorage.documents/tree/primary%3ADCIM. URI is that of a folder on external SD card.
Check the lines below
B4X:
Cursor1 = cr.Query(Uri1, Proj, "", Null, "")
Log(Cursor1.RowCount)
B4X:
Error occurred on line: 222 (Test)
java.lang.RuntimeException: Object should first be initialized (Cursor).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at anywheresoftware.b4a.sql.SQL$CursorWrapper.getRowCount(SQL.java:335)
at b4a.chrjak.sdcard.test._getpathfromcontentresult(test.java:661)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
** Service (starter) Destroy **
Point to note is I am trying to get path from URI of a folder and not file. Not sure if that is what is causing the error.