How do I browse an image using my program and make it visible?

IamBot

Member
Licensed User
Longtime User
Hi,

I am a total newbie so I need a pretty detailed explanation.

I have so far added an 'ImageView' from the designer where I want the browsed picture to appear and a button below it called 'Browse'. The user should click the button in order to browse the picture and make it appear in the program.

I have no code so far so there is nothing to post, unfortunately.

Would be very thankful for answers.
 

derez

Expert
Licensed User
Longtime User
This will be just a short explanation but I hope it will tell you what to look for.
There are two parts to your "problem":
1. browse, we use here the name "file dialog" and you can start by reading this http://www.b4x.com/forum/basic4andr...android-views-controls-dialogs.html#post41804 and following the links to the library. It makes a difference if you know the directory in advance or want to browse the whole file system.
2. After selecting the file, you have the directory and the file name. You can set the image to the imageview by using this command- [imgviewname].SetBackgroundImage(loadbitmap(Dir,filename))

Of course the size of the imageview and the size of the bitmap may not be the same, and you have to fix it. If you set the imageview Gravity to Fill - the bitmap will be stretched or shrunk to fit the size, but the ratio of width and height will change, so you'll probably need to define a canvas and to draw the bitmap on the imageview, setting the size accordingly.
 
Upvote 0

IamBot

Member
Licensed User
Longtime User



Thank you very much for the great help! I managed to download the libary and get the FileDialog to work. This is "my" code:

Sub ProfileBrowse_Click
Bmp.Initialize(File.DirAssets, "android48.png")
Dim fd As FileDialog
fd.FastScroll = True
'fd.ShowOnlyFolders = True
fd.FilePath = File.DirRootExternal ' also sets ChosenName to an emtpy string
'fd.ShowOnlyFolders = true
'fd.FileFilter = ".txt" ' for example or ".jpg,.png" for multiple file types
ret = fd.Show("B4A File Dialog", "Yes", "No", "Maybe", Bmp)
ToastMessageShow(ret & " : Path : " & fd.FilePath & CRLF & "File : " & fd.ChosenName, False)

[ProfilePic].SetBackgroundImage(LoadBitmap(Dir,filename))
End Sub

******

I copied the code you suggested '[imgviewname].SetBackgroundImage(loadbitmap(Dir,filename))'
but how do I get this to work with my program?


The name of my ImageView file is 'ProfilePic' as you can see, but how do I make "my" code work with what you suggested?

Thank you in advance.
 
Upvote 0

IamBot

Member
Licensed User
Longtime User
No problems now! I made it work

Sub ProfileBrowse_Click
Bmp.Initialize(File.DirAssets, "legohead.png")
Dim fd As FileDialog
fd.FastScroll = True
'fd.ShowOnlyFolders = True
fd.FilePath = File.DirRootExternal ' also sets ChosenName to an emtpy string


'fd.ShowOnlyFolders = True

'fd.FileFilter = ".jpg,.png"
'fd.FileFilter = ".txt" ' for example or ".jpg,.png" for multiple file types
ret = fd.Show("Choose a profile picture", "Yes", "No", "Maybe", Bmp)
ToastMessageShow(ret & " : Path : " & fd.FilePath & CRLF & "File : " & fd.ChosenName, False)

ProfilePic.SetBackgroundImage(LoadBitmap(fd.FilePath, fd.ChosenName ))
End Sub
 
Upvote 0

derez

Expert
Licensed User
Longtime User
The brackets were just to say "use your own view's name":
ProfilePic.SetBackgroundImage(LoadBitmap(fd.FilePath, fd.ChosenName))
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…