Android Question URI content://com.android ... to real file

Gregg Homan

Member
Licensed User
Longtime User
Hello,

Can someone explain to me why GetPathFromContentResult(UriString As String) is crashing (see code below) when trying to convert content://com.android... uri format into a real files?

http://www.b4x.com/android/forum/threads/uri-content-media-to-real-file.13473/#post-76176 explains how to use GetPathFromContentResult() to convert content://media... uri format into a real file.

I am using ContentChooser to select the same exact image via three different choices: Gallery, Photos and Images. The result from ContentChooser when opening a test image with Gallery and Photo is content://media/external/images/media/7567. The result from ContentChooser when opening same test image with Images is content://com.android.providers.media.documents/document/image%3A7567. GetPathFromContentResult() was able to convert content://media/external/images/media/7567 to /storage/emulated/0/DCIM/FrontCam/A12610.jpg. However, GetPathFromContentResult() crashes (see code below) when trying to convert content://com.android.providers.media.documents/document/image%3A7567

Thanks,
Gregg
B4X:
Sub GetPathFromContentResult(UriString As String) As String
  If UriString.StartsWith("/") Then Return UriString 'If the user used a file manager to choose the image
  Dim Proj() As String
  Proj = Array As String("_data")
  Dim Cursor As Cursor    'GGH 01-05-13, Requires Lib SQL (version 1.20)
  Dim r As Reflector
  Dim Uri As Object
  Uri = r.RunStaticMethod("android.net.Uri", "parse", _
      Array As Object(UriString), _
      Array As String("java.lang.String"))
  r.Target = r.GetContext
  r.Target = r.RunMethod("getContentResolver")
 
  'FAILS ON NEXT LINE WHEN PROCESSES content://com.android... format
  Cursor = r.RunMethod4("query", _
  Array As Object(Uri, Proj, Null, Null, Null), _
  Array As String("android.net.Uri", _
      "[Ljava.lang.String;", "java.lang.String", _
      "[Ljava.lang.String;", "java.lang.String"))
 
  Cursor.Position = 0
  Dim res As String
  res = Cursor.GetString("_data")
  Cursor.Close
  Return res
End Sub

Here is the error message that code above throws when it tries to process a content://com.android... uri format:
B4X:
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaDocumentsProvider uri content://com.android.providers.media.documents/document/image%3a7567 from pid=10761, uid=10229 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()
    at android.os.Parcel.readException(Parcel.java:1461)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
    at android.content.ContentProviderProxy.query(ContentProviderNative.java:413)
    at android.content.ContentResolver.query(ContentResolver.java:461)
    at android.content.ContentResolver.query(ContentResolver.java:404)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.agraham.reflection.Reflection.runmethod(Reflection.java:216)
    at anywheresoftware.b4a.agraham.reflection.Reflection.RunMethod4(Reflection.java:857)
    at Ovinion.Ovinion.main._getpathfromcontentresult(main.java:3156)
    at Ovinion.Ovinion.main._chooser_result(main.java:1845)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:173)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:157)
    at anywheresoftware.b4a.phone.Phone$ContentChooser$1.ResultArrived(Phone.java:843)
    at anywheresoftware.b4a.BA$4.run(BA.java:477)
    at anywheresoftware.b4a.BA.setActivityPaused(BA.java:388)
    at Ovinion.Ovinion.main$ResumeMessage.run(main.java:252)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5083)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
    at dalvik.system.NativeStart.main(Native Method)

I also tried adding following code to the Manifest file but it does not stop above error from occurring:
B4X:
AddPermission("android.permission.MANAGE_DOCUMENTS")

Also, I am not sure how to implement grantUriPermission() via code or manifest file as suggested on first line of the above error.
 
Top