Android Question Upload photos from the gallery and display in ImageView

red30

Well-Known Member
Licensed User
Longtime User
I want to choose several photos (e.g. four items) having pushed the button then I want them to be reflected in ImageView 2×2. How to do it?
 

DonManfred

Expert
Licensed User
Longtime User
There is no such Object (the imageslist with the Xses) in B4A.

BUT
You can use Contentresolver to get the selected items.
You need to build the list of imageviews (in a xcustomlistview for ex) by yourself.
But you get the path and content-reference in the Event to load the images...
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
Can anyone give an example? I want to select multiple images and display them on imageview...
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
Pushing the button I want a chosen photo be added to according ImageView. Then I choose another photo and it is added nearby and etc. If you push a photo, it is deleted. How to do it?
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    Private chooser As ContentChooser
    Type ItemValue2 (Lanel2 As Label, ImageView2 As ImageView)
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private CLV1 As CustomListView
    Private Label2 As Label
    Private ImageView2 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
    Dim iv2 As ItemValue2
    iv2.Initialize
    CLV1.Add(CreateItem2(iv2,$"Фото 1:"$),iv2)
    CLV1.Add(CreateItem2(iv2,$"Фото 2:"$),iv2)
End Sub

Private Sub CreateItem2 (iv2 As ItemValue2, ti2 As String) As B4XView
    Dim p2 As B4XView=xui.CreatePanel("")
    p2.SetLayoutAnimated(0,0,0,100%x,180dip)
    p2.LoadLayout("Item2")
    iv2.Lanel2=Label2
    iv2.Lanel2.Text=ti2
    iv2.ImageView2=ImageView2
    Return p2
End Sub

Sub Button2_Click
    Dim index As Int= CLV1.GetItemFromView(Sender)
    Log    ($"Button #${index}"$)
   
    chooser.Initialize("chooser")
    chooser.Show("image/*", "Choose image")
End Sub

Sub chooser_Result (Success As Boolean, Dir As String, FileName As String)
    If Success Then
        'Dim bmp As Bitmap
        'bmp.Initialize(Dir, FileName)
        'Activity.SetBackgroundImage(bmp)
        Dim index As Int= CLV1.GetItemFromView(Sender)
        Dim iv2 As ItemValue2 = CLV1.GetValue(index)
        iv2.ImageView2=ImageView2
        ImageView2.Bitmap = LoadBitmap(Dir, FileName)
    Else
        ToastMessageShow("Фото не выбрано...", True)
    End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

All project: https://cloud.mail.ru/public/6k4T/UcGnUHdfD
 

Attachments

  • 11111111121111.jpg
    11111111121111.jpg
    25.3 KB · Views: 402
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Pushing the button I want a chosen photo be added to according ImageView. Then I choose another photo and it is added nearby and etc. If you push a photo, it is deleted.
This is an app description, not a question.

What is the question?

You can use xCustomListView with a horizontal orientation for this layout.
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
Sorry, I forgot.
The question is that I cant add the pictures with ContentChooser to the necessary Imageview. They are added to the last (lowest) Imageview or the app shows an error:
B4X:
Logger connected to: 12184415843991
--------- beginning of /dev/log/main
--------- beginning of /dev/log/system
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Class not found: b4a.example.customlistview, trying: b4a.example.test25.customlistview
** Activity (main) Resume **
Button #0
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
customlistview_getitemfromview (java line: 413)
java.lang.ClassCastException: anywheresoftware.b4a.phone.Phone$ContentChooser cannot be cast to android.view.View
    at anywheresoftware.b4a.objects.B4XViewWrapper.asViewWrapper(B4XViewWrapper.java:53)
    at anywheresoftware.b4a.objects.B4XViewWrapper.getParent(B4XViewWrapper.java:122)
    at b4a.example.test25.customlistview._getitemfromview(customlistview.java:413)
    at b4a.example.test25.main._chooser_result(main.java:400)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at anywheresoftware.b4a.phone.Phone$ContentChooser$1.ResultArrived(Phone.java:843)
    at anywheresoftware.b4a.BA$4.run(BA.java:566)
    at anywheresoftware.b4a.BA.setActivityPaused(BA.java:432)
    at b4a.example.test25.main$ResumeMessage.run(main.java:305)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5281)
    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:932)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:748)
    at dalvik.system.NativeStart.main(Native Method)

How can I add the pictures to the definete (necessary to me) Imageview?
You can use xCustomListView with a horizontal orientation for this layout.
Please could you give any example?
 
Upvote 0

Johan Hormaza

Well-Known Member
Licensed User
Longtime User
Here is an example of how it works with xCustomListView, although it is not exactly the same as the video, but you can start with this code
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("ImagenFoto")
    swith.Initialize("")
    barrFoto.AddView(swith,20%x,10%x,Gravity.RIGHT)
    ProgressDialogShow2("Cargando las fotos...",False)
    FotoFileDir = rp.GetSafeDirDefaultExternal("Folder") & "/Fotos/"
    LoadImagen
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
Activity.Finish
End Sub

Sub CLV1_VisibleRangeChanged (FirstIndex As Int, LastIndex As Int)
    Dim ExtraSize As Int = 20
    For i = 0 To CLV1.Size - 1
        Dim p As B4XView = CLV1.GetPanel(i)
        If i > FirstIndex - ExtraSize And i < LastIndex + ExtraSize Then
            'visible+
            If p.NumberOfViews = 0 Then
                Dim fl As FotosL = CLV1.GetValue(i)
                p.LoadLayout("pnlImagen")
                lblTitle.Text = fl.Nombre
                ImageView1.SetBitmap(xui.LoadBitmapResize(FotoFileDir, fl.ImagenArchivo, 100%x, 100%y, True))
            End If
        Else
            'Not visible
            If p.NumberOfViews > 0 Then
                p.RemoveAllViews '<--- remove the layout
            End If
        End If
        Sleep(0)
    Next
    ProgressDialogHide
End Sub

Sub LoadImagen
    Try
        Dim files As List
        files.Initialize
        files = File.ListFiles(FotoFileDir)
        If files.Size = 0 Then
            ToastMessageShow("No se ha encontrado imagenes " & CRLF & FotoFileDir, True)
            ProgressDialogHide
        Else
            For i = 0 To files.Size - 1
                Dim FL As FotosL
                FL.Initialize
                FL.Nombre = files.Get(i)
                FL.ImagenArchivo = files.Get(i)
                Dim p As B4XView = xui.CreatePanel("")
                p.SetLayoutAnimated(0, 0, 0, CLV1.AsView.Width, 100%y)
                CLV1.Add(p,FL)
            Next
        End If
    Catch
        Log(LastException)
    End Try
End Sub
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
This is not exactly what I wanted. Can you lay out the whole project? I do not understand how it works. I do not understand how, with the help of xCustomListView in when the button is clicked, to display the image under this button in the ImageView?
 
Upvote 0

Addo

Well-Known Member
Licensed User
Longtime User
create a content chooser

B4X:
Dim Chooser As ContentChooser

Chooser.Initialize("chooser")

'and you can use this chooser in any click event

Chooser.Show("image/*", "Choose image")


'now grab the file path

Sub chooser_Result (Success As Boolean, Dir As String, FileName As String)
If Success Then
'if choosen use loadbitmap to load image from path
'LoadBitmap(Dir,FileName)// file path

' you can pass a sub to load that file to xcustomlist view here
end if
End sub

if you want to show this xclv vertically you should create a layout to hold images in one row

as example 4 images in layout then each item will hold 4 image
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
I still can not figure out how to add a photo to the corresponding ImJVew. They always add to the last. What am I doing wrong? How to scale a photo 100х100?
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim Chooser As ContentChooser
    Private MyCLV As CustomListView
    Private Button1 As Button
    Private CLV_IV As CustomListView
    Private ImageView1 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Main")
    Chooser.Initialize("chooser")
    For i = 1 To 20
    MyCLV.Add(CreateListItem("MyText"&i),i)
    Next
End Sub

Sub CreateListItem(Text As String) As Panel
    Dim p As Panel
    p.Initialize("")
    p.SetLayout(0, 0, 100%x, 150dip)
    p.LoadLayout("Item")
    Button1.Text = Text
    Return p
End Sub

Sub CreateListItem2(btm As Bitmap) As Panel
    Dim p As Panel
    p.Initialize("")
    p.SetLayout(0, 0, 100dip, 100dip)
    p.LoadLayout("Item_IV")
    ImageView1.Bitmap=btm
    Return p
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    Dim index As Int = MyCLV.GetItemFromView(Sender)
    Dim pnl As B4XView = MyCLV.GetPanel(index)
    Dim clv As B4XView= pnl.GetView(1)
    Chooser.Show("image/*", "Choose image")
    Log("Checked items: " & index)
    'Dim lbl As B4XView = pnl.GetView(0)
    'Dim chk As B4XView = pnl.GetView(2)
End Sub

Sub chooser_Result (Success As Boolean, Dir As String, FileName As String)
    If Success Then
        'if choosen use loadbitmap to load image from path
        CLV_IV.Add(CreateListItem2(LoadBitmapResize(Dir,FileName,100dip,100dip,True)),0)
        'LoadBitmap(Dir,FileName)
        ' you can pass a sub to load that file to xcustomlist view here
    End If
End Sub
 
Last edited:
Upvote 0
Top