Android Question WhatsApp Uri how to get the image

Alexander Stolte

Expert
Licensed User
Longtime User
Hey,

if i share a image with my app from WhatsApp, then the uri looks so:
B4X:
content://com.whatsapp.provider.media/item/705a29e3-195e-47ad-97de-adebcedb83e0
Normally I get all paths with this code:
B4X:
Public 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
but there this error occurs:
B4X:
java.lang.IllegalArgumentException: column '_data' does not exist. Available columns: []

What do I have to do to get the right path?
 

JohnC

Expert
Licensed User
Longtime User
URI's are not guaranteed to be able to be decoded to a local dir/path.

Try this to receive the file:

 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
with this code i only gets the filename, but without path is it worthless.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Try using Erel's code that that thread was based on to receive the actual file:


Then using both threads you can receive the file and find out the original file name for it.
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
That's not working, also for the images outside of whatsapp.
Then using both threads you can receive the file and find out the original file name for it.
How? i have not found a possibility?

Do you want to load the image?
B4X:
Dim bmp As B4XBitmap = XUI.LoadBitmap("ContentDir", Uri)
B4X:
java.io.FileNotFoundException: ContentDir/content:/com.whatsapp.provider.media/item/7391eb74-0230-4d06-ab7e-532da4bcdfe5: open failed: ENOENT (No such file or directory)

Code based on GetPathFromContentResult = broken code.
Outside Whatsapp this code works reliably on all devices + emulators where I have tested it.
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
I did it!

It's simple, same handle as on the ContentChooser.
B4X:
Dim in As JavaObject = Activity.GetStartingIntent
Dim uri As String = in.RunMethod("getParcelableExtra", Array("android.intent.extra.STREAM")
Wait For (File.CopyAsync("ContentDir", uri, File.DirInternalCache, "tmp_img.jpg")) Complete (Success As Boolean)
Dim bmp As B4XBitmap = XUI.LoadBitmap(File.DirInternalCache, "tmp_img.jpg")
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I'm thinking it worked because the file was copied first before trying to load it as a bitmap.

Try this:
B4X:
File.Copy("ContentDir",Uri,File.DirInternal,"somepicture.bmp")
Dim bmp As B4XBitmap = XUI.LoadBitmap(File.DirInternal, "somepicture.bmp")
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
with this code i only gets the filename, but without path is it worthless.
Now that you can get the file, if it would be a benefit to display/know the name of the image file that was sent from Whatsapp, then you can use the code from here to get it:

 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…