Android Tutorial Getting picture from gallery

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use a ContentChooser object to do it. Make sure to add a reference to the Phone library.
B4X:
Sub Process_Globals
    Dim chooser As ContentChooser
End Sub

Sub Globals
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        chooser.Initialize("chooser")
    End If
    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)
    Else
        ToastMessageShow("No image selected", True)
    End If
End Sub
 

niqpw

Member
Licensed User
Longtime User
Hi

I've successfully used this to select a file. However I'm a bit stuck on using the Dir and Filename returned with the AB FTP library. The file and folder strings are not recognised, any ideas?

Thanks in advance!
 

niqpw

Member
Licensed User
Longtime User
Hi

Thanks Erel, but still stuck!

I'm sure I'm missing something simple here, but when I try the following, the file does not exist with the strings passed in.

Dir = ContentDir
FileName = content://media/external/images/media/2

B4X:
Sub CC_Result (Success As Boolean, Dir As String, FileName As String)
    If Success Then
   If File.Exists(Dir,FileName) Then
      Msgbox("File exists","")
   End If
    Else
        ToastMessageShow("No image selected", True)
    End If
End Sub

Thanks again!
 

niqpw

Member
Licensed User
Longtime User
Thanks Erel for pointing me in the right direction. For info, I've found this does what I want:

B4X:
Dim bmp As Bitmap
bmp.Initialize(i_strDir, i_strFileName)
Dim objOut As OutputStream
objout = File.OpenOutput(File.DirRootExternal,"test.png",False)
bmp.WriteToStream(objout,100,"PNG")
objout.Close

Cheers!
 

aloof

Member
Licensed User
Longtime User
Hi Im using this code and getting this error

an Error has occurred in sub: main$resumemessagerun (javaline: 190)
java.lang.exception:Sub
Chooser_result signature does not match expected signature, continue?


anyone know what the problem might be?

Thanks
 

aloof

Member
Licensed User
Longtime User
Thanks, i got passed it.. i changed the code that much im not sure what i did to fix it.

Thanks anyway
 

Yamato

New Member
Licensed User
Longtime User
Hi, i had the same problem, in my case the
"bmp.initialize(Dir,Filename)" was giving me an unexpected close of the aplication when i try to open certain images, so i've tried to copy the file using the "File.Copy2" with an inputstream and the outputstream and it worked well.
Here i put the code i've used :

B4X:
Sub IMGdisplay_Result (Success As Boolean, Dir As String, FileName As String)

If Success = True Then
   Dim out As OutputStream
   Dim in As InputStream
   Dim l As List
      
   l.Initialize
      
   l = File.ListFiles(data.img_path & "/" & data.img_folder)
   in = File.OpenInput(Dir,FileName)
   out = File.OpenOutput(data.img_path & "/" & data.img_folder,"I" & (l.Size + 1),False)
      
   File.Copy2(in,out)
   out.Close
End If

End Sub

I'm using the "size + 1" to give a new name to the file because the Filename gives me an error if i put it in almost everywhere

I hope it can be of help
 

irda

Member
Licensed User
Longtime User
There's any way to select a directory? I only need the directory path to write some file but only found librarys that's not work 100% well

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