Android Code Snippet Get the path to media files returned from ContentChooser

pesquera

Active Member
Licensed User
Longtime User
Hi Erel, I need to copy an image file (photo) to my app folder with File.Copy2..
I have this error:
An error has occured in sub: javai.io.FileNotFoundException: no content provider: /storage/emulated/0/DCIM/Camera/20150222_204549.jpg
with this code:
B4X:
Sub glo_ContentChooser_Result (Success AsBoolean, Dir AsString, FileName AsString)
If Success Then

Dim jo As JavaObject
Dim cd As String = jo.InitializeStatic("anywheresoftware.b4a.objects.streams.File").GetField("ContentDir")
Dim UriString As String = GetPathFromContentResult(FileName)
Dim In As InputStream = File.OpenInput(cd , UriString)     
...
Can you tell me what I'm doing wrong? I've just replaced the line that gets the file
Watching on the UriString Variable, I can see this value:
/storage/emulated/0/DCIM/Camera/20150222_204549.jpg
Isn't that value correct?
Thanks
 
Last edited:

driesvp

Member
Licensed User
Longtime User
When using "GetPathFromContentResult" I receive an error on the line "Cursor1.Position = 0". The error states that Cursor1 should be initialised first.
How to solve this?
 

driesvp

Member
Licensed User
Longtime User
Does this only work for media files? I tried with google drive but receive errors.
 

walterf25

Expert
Licensed User
Longtime User
Hi All, i'm writing an Email application, i need to be able to attach any type of files when sending new emails or replying, i've tested Erel's code here but this only seems to work with image files, can anyone help me figure out how to get the full file path for let's say a pdf file or a text file, or even a zip file?

Thanks all in advanced.
Regards,
Walter
 

walterf25

Expert
Licensed User
Longtime User
Please start a new thread for this question.
Never mind, i created a small library from some code i found online that takes care of this problem, i will post it soon.

Regards,
Walter
 

Inman

Well-Known Member
Licensed User
Longtime User

The code is crashing when testing on Android 7.0. Tested uri was (HierarchicalUri) content://com.android.externalstorage.documents/tree/primary%3ADCIM. URI was obtained with the code given here.
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 **
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…