Android Question Using Photos from the Phone

hibrid0

Active Member
Licensed User
I try your code, And I dont use SQL in my App, I add SQL Library and say "java.lang.RuntimeException: Object should first be initialized (Cursor)."



You can use this code to get the full path (if such is available):
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 Cursor1 As Cursor
  Dim Uri1 As Uri
  Dim Proj() As String = Array As String("_data")
  Dim cr As ContentResolver
  cr.Initialize("")
  If UriString.StartsWith("content://com.android.providers.media.documents") Then
  Dim i As Int = UriString.IndexOf("%3A")
  Dim id As String = UriString.SubString(i + 3)
  Uri1.Parse("content://media/external/images/media")
  Cursor1 = cr.Query(Uri1, Proj, "_id = ?", Array As String(id), "")
  Else
  Uri1.Parse(UriString)
  Cursor1 = cr.Query(Uri1, Proj, "", Null, "")
  End If
  Cursor1.Position = 0
  Dim res As String
  res = Cursor1.GetString("_data")
  Cursor1.Close
  Return res
End Sub
It depends on the following libraries: ContentResolver, SQL and Phone.
 

Erel

Administrator
Staff member
Licensed User
This means that the file path is not available. Note that in most cases you do not need this path to work with the selected image.
 

Marco Nissen

Active Member
Licensed User
This means that the file path is not available. Note that in most cases you do not need this path to work with the selected image.
Well, it seems as if the file path is a security issue for the latest update of Android. Instead, you should use the input stream via content resolver / openinputstream .. small snippet:

B4X:
Sub GetStreamFromContentResult(UriString As String) As InputStream
    Dim r As Reflector
    Dim Uri As Object
    Dim iStream As InputStream
    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")
    iStream = r.RunMethod4( "openInputStream", Array As Object(Uri), Array As String("android.net.Uri"))
    Return iStream
End Sub
With this, you can easily receive e.g. a PDF

B4X:
Try
                         Dim theTitle As String =getTitleFromContent(ShareString)  ' the approach above using _display_name
                        Dim thePath As String = ah.getTemporaryDirectory ' my way to obtain a writeable path
                        Dim jo As JavaObject
                        Dim Inp As InputStream  = GetStreamFromContentResult(ShareString)
                        Dim Out As OutputStream = File.OpenOutput(thePath, theTitle, False)
                        File.copy2(Inp, Out)
                        Inp.Close
                        Out.Close
                                                receivedPDF = thePath & "/" & theTitle
                    Catch
                        Log(":: issue " & LastException)
                    End Try
 
Top