Android Question Save image links in the database and open them in a listview

WebQuest

Active Member
Licensed User
Precedentemente ho postato una domanda sull'inserimento di immagini di BLOB in un database e sul caricamento in un listview.
QUI: Salvare un'immagine dalla galleria di smartphone nel database e caricarla in un listview.

Ora come i BLOB sqllite accettano solo uno dei due formati di immagine e Android limita il caricamento di super immagini a 2mb ecc. Come descritto in una domanda. Voglio provare a salvare l'immagine selezionata dalla galleria di smatphone e salvarla in DirAsset e salvare il collegamento nel database per visualizzare le immagini in un listview, ma non riesco a trovare la strada! Ho provato alcune soluzioni ma non sono buone. Come fare un esempio?
 
Last edited:

ronell

Well-Known Member
Licensed User
it is not possible to write anything in dirassets, use RuntimePermissions.GetSafeDirDefaultExternal("") instead .. see this https://www.b4x.com/android/forum/threads/runtime-permissions-android-6-0-permissions.67689/

you don't need to save the link or full path of the image you want to save in your db, just the image name will do .. use ContentChooser to save the images from gallery search the forum for examples

to display the image , get the GetSafeDirDefaultExternal path + the image name

B4X:
dim dir as string = RuntimePermissions.GetSafeDirDefaultExternal("")

Dim bmp As Bitmap = LoadBitmap(dir,"image.png")

imgview.Bitmap = bmp
 

Mahares

Well Known Member
Licensed User
Dim bmp As Bitmap = LoadBitmap(dir,"image.png")
Hi @ronell
You really do not need to copy the images to any other folder. They can stay in the assets folder and do this to extract the image:
B4X:
Dim bmp As Bitmap = LoadBitmap(File.DirAssets,"image.png")
or you can load them to the listview, unless of course he plans to modify the images.
 

WebQuest

Active Member
Licensed User
The real problem is this: I save the images jpg and jpeg in the db converting them in bytes. but I get an error loading the jpeg blobs saved in the db. Not finding the solution I thought that saving the image in the DirInternal and saving the link in the db I would have solved the problem of loading.
 

ronell

Well-Known Member
Licensed User
You really do not need to copy the images to any other folder.
the images he wants to display will not come from the assets folder.


The real problem is this: I save the images jpg and jpeg in the db converting them in bytes. but I get an error loading the jpeg blobs saved in the db. Not finding the solution I thought that saving the image in the DirInternal and saving the link in the db I would have solved the problem of loading.
see #2 post .. use contentchooser to open gallery and wait for the result sub, get the image file then save it on other directory and also save the image name in your local db
 

WebQuest

Active Member
Licensed User
Hello ronelle, thanks for the advice is what I try to do but I can not you could show me an example?
 

WebQuest

Active Member
Licensed User
count to receive this error at the row load : java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
 

npsonic

Active Member
Licensed User
If I understand correctly, this is what you want?

images: KeyValueStore2, clv: CustomListView and picasso: Picasso

B4X:
Sub Chooser_Result (Success As Boolean, Dir As String, FileName As String)
    If Success Then
        'Without copy
        images.Put(DateTime.Now,File.Combine(Dir,FileName))
        
        'With copy
        'images.Put(DateTime.Now,File.Combine(File.Combine(File.DirInternal,"images"),FileName))
        'File.Copy(Dir,FileName,File.Combine(File.DirInternal,"images"),FileName)
    Else
        Log("No image selected")
    End If
End Sub

Sub FillCLV
    Dim keys As List = images.ListKeys
    
    Dim xui As XUI
    
    For Each key In keys
        Dim p As B4XView = xui.CreatePanel("")
        p.SetLayoutAnimated(0, 0, 0, clv.AsView.Width, 100dip)
        p.Color = Colors.Transparent
        clv.Add(p,images.Get(key))
    Next
    
    keys.Clear
End Sub

Sub CLV_VisibleRangeChanged (FirstIndex As Int, LastIndex As Int)
    Dim ExtraSize As Int = 20
    For i = 0 To clv.Size - 1 Step 1
        Dim p As B4XView = clv.GetPanel(i)
        If i > FirstIndex - ExtraSize And i < LastIndex + ExtraSize Then
            'visible+
            If p.NumberOfViews = 0 Then
                Dim iv As ImageView : iv.Initialize("")
                p.AddView(iv,(p.Width - p.Height) / 2,0,p.Height,p.Height)
                picasso.LoadFile(clv.GetValue(i)).IntoImageView(iv)
            End If
        Else
            'not visible
            If p.NumberOfViews > 0 Then
                picasso.CancelRequest(p.GetView(0))
                p.RemoveAllViews '<--- remove the layout
            End If
        End If
    Next
End Sub
 
Top