Android Question How to get images from device

thedesolatesoul

Expert
Licensed User
I thought the correct way to get images from the device is to query the media store.
For some reason I cannot get it to work!

B4X:
Sub Activity_Create(FirstTime As Boolean)
    CR.Initialize("cr")
    Dim image_uri As Uri
    image_uri.Parse("content://media/external/images/media")
    Dim projection() As String = Array As String("datetaken")
    CR.QueryAsync(image_uri, projection, Null, Null,Null)

End Sub

Sub CR_QueryCompleted (Success As Boolean, Crsr As Cursor)
    Log("Success")
    Log(Crsr.RowCount & " Rows")
    For i = 0 To Crsr.RowCount-1
        Dim s As StringBuilder
        s.Initialize
        For j = 0 To Crsr.ColumnCount-1
            s.Append(Crsr.GetString2(j) & " ")
        Next
        Log(s)
    Next
End Sub

Also, I am aware there is the Mediabrowser library, but it is not working with some devices, so trying to track down the issue, I came to querying the media store myself.
 

Attachments

  • Images.zip
    6.4 KB · Views: 74

thedesolatesoul

Expert
Licensed User
Upvote 0

stevel05

Expert
Licensed User
How about if you put it in a sub and use Thread RunOnGuiThread method, just out of curiosity.
 
Upvote 0

Informatix

Expert
Licensed User
Me neither, but this isnt working on certain devices (so far all are 4.4)
https://www.dropbox.com/s/p43xlvztp5ue02u/PicPuzzle.apk?dl=0

I use:
B4X:
    ImageMap = MB.GetMediaImageList(True,  "datetaken COLLATE UNICODE DESC limit " & ImageLimit & " offset " & Offset)

(permissions = manage_documents,read_external_storage)
Here's an example that runs without issues under Kitkat:
http://www.basic4ppc.com/android/forum/attachments/customgallery-mediabrowser-zip.28521/

Since 4.4, some files cannot be accessed due to the new Google policy about files on SD card. In this case, functions like GetImgThumbnailByID return an uninitialized bitmap. When you browse the result map, you have to skip these files, e.g.:
B4X:
Dim bmpImage As Bitmap = MB.GetImgThumbnailByID(Info.ID, False)
If Not(bmpImage.IsInitialized) Then Continue
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Ok, I found the problem...its embarassing! (A wise man once told me the problem was in my code!)
I even tried both of your suggestions (the check for bmp.isInitialized is also necessary).

For completeness I will add my code here:

B4X:
Sub ReadLocalFiles(Offset As Int,ImageLimit As Int)
    Dim ImageMap As Map
    MB.Initialize("MB")
    ImageMap = MB.GetMediaImageList(True,  "datetaken COLLATE UNICODE DESC limit " & ImageLimit & " offset " & Offset)
    PicMap.Initialize
    For i = 0 To (ImageMap.Size/7)-1
        Dim subMap As Map     : subMap.Initialize
        subMap.Put("Location",ImageMap.get("Location" & i))
        subMap.Put("Thumbnail","")
        PicMap.Put(ImageMap.get("ID" & i), subMap)
    Next
    'Log(PicMap)
    If pCacheThumbs = True Then
        'Thumbnails
        For i = 0 To PicMap.Size-1
            Dim ID As Long =  PicMap.GetKeyAt(i)
             Dim subMap As Map =  PicMap.GetValueAt(i)
            Log(ID & " " & subMap)
            Dim bmpThumb As Bitmap = MB.GetImgThumbnailByID(ID,False)
            'Log("Fetched bmpThumb")
            If bmpThumb.IsInitialized AND bmpThumb <> Null Then
                'Log(bmpThumb)
                subMap.Put("Thumbnail",bmpThumb)
                'Log("Put into map")
            Else
                subMap.Put("Thumbnail",Null)
            End If
        Next
    End If
    Log("End ReadLocalFiles")
End Sub

My bug was, my for loop ran from (0 to ImageLimit) instead of from (0 to PicMap.Size). Any device where number of pictures were less than ImageLimit was therefore failing as going into a false loop.

MediaBrowser WORKS!

Thank you!
 
Upvote 0
Top