choose only image files

Yeshua

Member
Licensed User
Longtime User
How you can use the chooser only to open image files (. jpg, png, jpeg, etc.)?

If you accidentally click on a file with a different extension (.txt, for example), the chooser crashes.

B4X:
Sub Process_Globals
   Dim loadpicture As ContentChooser
End Sub

Sub Globals
   
   Dim pnlempty As Panel
   Dim image1 As ImageView

   Dim bitmap1,bitmap2 As Bitmap
   Dim Canva1 As Canvas
   Dim RectDest As Rect
   
End Sub



Sub Activity_Resume

End Sub


Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub Button1_Click
   loadpicture.Initialize("chooser")
   loadpicture.Show("image/*", "Choose image")
End Sub


Sub chooser_Result (Success As Boolean, Dir As String, FileName As String)

   If Success Then
   
   bitmap1.InitializeSample(Dir, FileName, pnlempty.Width, pnlempty.Height)
   bitmap2.InitializeMutable(pnlempty.Width,pnlempty.Height)
   Canva1.Initialize2(bitmap2)
   RectDest.Initialize(0, 0, pnlempty.Width, pnlempty.Height)
   Canvas1.DrawBitmap(bitmap1, Null,RectDest)
   empty.Bitmap = bitmap2

   End If

End Sub
:sign0148:
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I don't think that the chooser crashes. The error probably happens when you try to load the bitmap. You can add Try Catch to avoid such errors.

The third party application is supposed to let you choose items based on the mime type. If however the user selects a file manager then it might allow him to choose any file.
 
Upvote 0
Top