Imageview related with sdcard getting images

Asmoro

Active Member
Licensed User
Longtime User
Hi,

I have an imageview that generally handles the click event getting
a picture from a sdcard and loading to a panel.

I manage to find images on my sdcard by clicking on a imageview using
ContentChooser activity, but unfortunately I get a error saying
'Error loading bitmap'.

It must be implementing the directory issue.

So I hope someone pointing me the right direction where it did go wrong.

I post a testcase, just to explore.

My images are stored in the sdcard, folder DCIM and subfolder 100ANDRO.

note: I use my SE X10 phone to test this code.

grtz
Asmoro
 

Attachments

  • imageviewtestcase.zip
    50.3 KB · Views: 316

kickaha

Well-Known Member
Licensed User
Longtime User
I presume you are selecting a file with contentchooser, and then wish to load it?

Your code:
B4X:
Sub chooser_Result (Success As Boolean, Dir As String, FileName As String)
    If Success Then
        Dim jpg As Bitmap
        jpg.Initialize(File.DirRootExternal,"DCIM/100ANDRO")
        Activity.SetBackgroundImage (LoadBitmap(File.DirRootExternal,"DCIM/100ANDRO"))
    Else
        ToastMessageShow("No image selected", True)
    End If
End Sub
is trying to load a file called "DCIM/100ANDRO" from the root of the sd card.

Why are you not using the file and directory selected with the contentchooser?
B4X:
Sub chooser_Result (Success As Boolean, Dir As String, FileName As String)
    If Success Then
        Dim jpg As Bitmap
        jpg.Initialize(Dir, FileName)
        Activity.SetBackgroundImage (LoadBitmap(Dir, FileName))
    Else
        ToastMessageShow("No image selected", True)
    End If
End Sub
 
Upvote 0

Asmoro

Active Member
Licensed User
Longtime User
Hi Kickaha,

Thanks to point out the direction, but after the correction, my proces stopped.

Does it mean that I have to resize the image for the imageview, let's say width=150, height=150
 
Upvote 0

Asmoro

Active Member
Licensed User
Longtime User
Thanks Erel, I will do that.

Another thing, do I rename the 'Dir' and 'Filename' to the actually directory and folder.
In my example: 'Dir' = File.DirRootExternal, folder = DCIM and subfolder = 100ANDRO.

so that will be : jpg.Initialize(File.DirRootExternal, "DCIM", "100ANDRO")
Activity.SetBackgroundImage(LoadBitmapSample(File.DirRootExternal,"DCIM", "100ANDRO"))

grtz
Asmoro
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Thanks Erel, I will do that.

Another thing, do I rename the 'Dir' and 'Filename' to the actually directory and folder.
In my example: 'Dir' = File.DirRootExternal, folder = DCIM and subfolder = 100ANDRO.

so that will be : jpg.Initialize(File.DirRootExternal, "DCIM", "100ANDRO")
Activity.SetBackgroundImage(LoadBitmapSample(File.DirRootExternal,"DCIM", "100ANDRO"))

grtz
Asmoro

You are getting in a muddle with directories and filenames.
'Dir' is the complete path to the directory, 'Filename is the name of the actual image.
the folder the file is in is part of the directory:
eg a file : SD/DCIM/100ANDRO/mypic.jpg
the directory is SD/DCIM/100ANDRO and the filename is mypic.jpg

If you are using contentchooser to select the file, the string 'Dir' will have the directory name, and the string 'FileName' will have the file that has been selected, so you do not need to replace them.
 
Last edited:
Upvote 0

Asmoro

Active Member
Licensed User
Longtime User
Kickaha,

I did your suggestion and no error came afterwards.
But still no image on my imageview and related error warning.

So I made 2 sub's like below and didn't work also.

Sub chooser_Result (Success As Boolean, Dir As String, FileName As String)
If Success Then
Dim jpg As Bitmap
jpg.Initialize(Dir, Filename)
Activity.SetBackgroundImage (LoadBitmapSample(Dir, Filename, 150dip, 150dip))
Else
ToastMessageShow("No image selected", True)
End If
End Sub


Sub ImageView1_Click

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

ImageView1 = Sender ' gets the View that raised the Click event


End Sub

Is there anything that I have to change to make it work, loading to my imageview.

Asmoro
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Can you use the debugger to single step through Sub chooser_Result to see what values are given to Dir and FileName. You can then check that these files exist.
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Make a new project and paste this code into the activity.
B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim cc As ContentChooser
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 GetImage As Button
   Dim ImageView1 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   cc.Initialize ("chooser")
   ImageView1.Initialize ("")
   GetImage.Initialize ("GetImage")
   ImageView1.Gravity = Gravity.Fill 
   Activity.AddView (ImageView1, 10dip, 100dip, 300dip, 300dip)
   GetImage.Text  = "Load Picture"
   Activity.AddView (GetImage,10dip, 10dip, 300dip, 60dip)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub GetImage_Click
   CC.Show("image/*", "Choose image")
End Sub
Sub chooser_Result (Success As Boolean, Dir As String, FileName As String)
   If Success Then ImageView1.Bitmap = LoadBitmap (Dir, FileName)
End Sub

When you press the button, you should be able to select an image from your gallery or SD card and it will be shown in the ImageView.

Dont forget to add the phone library.

Let me know if it works.
 
Upvote 0

Asmoro

Active Member
Licensed User
Longtime User
Kickaha,

The test result with a little tweak:

- getting image from my sdcard works, but if I want to change the image,
the proces stopped.

So I changed 'If Success Then ImageView1.Bitmap = LoadBitmap (Dir, FileName)'
to 'If Success Then ImageView1.Bitmap = (LoadBitmapSample (Dir, FileName, 300dip, 300dip))', making the image size smaller.

Now it works, thanks to you.

I would like to put this example in the tutorial section for the audience,
with your permission.

Hear from you soon.

grtz
Asmoro
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Glad you got it sorted finally.
Go ahead and add it to wherever you want, I just threw it together to show you a working version.
 
Upvote 0

Asmoro

Active Member
Licensed User
Longtime User
Kickaha,

Before I put this in the tutorial section, I made another test.

This time I made 2 imageviews and after selecting an image from the sdcard
by clicking an imageview (not button), I get the same image on both imageviews.

Do you have a solution for this behaviour.

Asmoro
 
Upvote 0

Asmoro

Active Member
Licensed User
Longtime User
Here you are:

Sub Process_Globals
Dim cc As ContentChooser
End Sub

Sub Globals
Dim ImageView1 As ImageView
Dim Imageview2 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("imagetest1.bal")

cc.Initialize ("chooser")
ImageView1.Initialize ("")
ImageView2.Initialize ("")

ImageView1.Gravity = Gravity.Fill
ImageView2.Gravity = Gravity.Fill

Activity.AddView (ImageView1, 165, 210, 150, 150)
Activity.AddView (ImageView2, 165, 405, 150, 150)

End Sub

Sub chooser_Result (Success As Boolean, Dir As String, FileName As String)
If Success Then ImageView1.Bitmap = (LoadBitmapSample (Dir, FileName, 150, 150))
If Success Then ImageView2.Bitmap = (LoadBitmapSample (Dir, FileName, 150, 150))
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub ImageView1_Click
cc.Show("image/*", "Choose image")
End Sub

Sub ImageView2_Click
cc.Show("image/*", "Choose image")
End Sub
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
In chooser_Result
B4X:
Sub chooser_Result (Success As Boolean, Dir As String, FileName As String)
If Success Then ImageView1.Bitmap = (LoadBitmapSample (Dir, FileName, 150, 150))
If Success Then ImageView2.Bitmap = (LoadBitmapSample (Dir, FileName, 150, 150))
End Sub

you are setting both imageview bitmaps, so every time you use the ContentChooser you will set both imageviews to the same image.

We can modify your code to have a pointer to the imageview you have clicked:
B4X:
Sub Process_Globals
Dim cc As ContentChooser
End Sub

Sub Globals
Dim ImageView1 As ImageView
Dim Imageview2 As ImageView
Dim ImageViewTemp As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("imagetest1.bal")

cc.Initialize ("chooser")
ImageView1.Initialize ("ImageView1")
ImageView2.Initialize ("ImageView2")

ImageView1.Gravity = Gravity.Fill
ImageView2.Gravity = Gravity.Fill

Activity.AddView (ImageView1, 165, 210, 150, 150)
Activity.AddView (ImageView2, 165, 405, 150, 150)

End Sub

Sub chooser_Result (Success As Boolean, Dir As String, FileName As String)
If Success Then ImageViewTemp.Bitmap = LoadBitmapSample  (Dir, FileName, 150, 150)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub ImageView1_Click
ImageViewTemp = ImageView1
cc.Show("image/*", "Choose image")
End Sub

Sub ImageView2_Click
ImageViewTemp = ImageView2
cc.Show("image/*", "Choose image")
End Sub

Not tested but it should work.
 
Upvote 0

Asmoro

Active Member
Licensed User
Longtime User
Kickaha,

Great job, it's working perfectly.

Now we have 2 versions to choose from.

One handling with a button and an imageview and the other one
a handling touching the imageview(s) only to get an image from a sdcard.

Thanks again and see you next time.

grtz.
Asmoro

Ps. I will put the examples in the tutorial section.
 
Upvote 0

Lordshiva1948

Member
Licensed User
Longtime User
Hi,

I have an imageview that generally handles the click event getting
a picture from a sdcard and loading to a panel.

I manage to find images on my sdcard by clicking on a imageview using
ContentChooser activity, but unfortunately I get a error saying
'Error loading bitmap'.

It must be implementing the directory issue.

So I hope someone pointing me the right direction where it did go wrong.

I post a testcase, just to explore.

My images are stored in the sdcard, folder DCIM and subfolder 100ANDRO.

note: I use my SE X10 phone to test this code.

grtz
Asmoro

hi there try it like this

I have written this for myself to see if it works on my sony xperia 8. It is working fine. I have tested it on my other phone Star A3000 Duel sim also. works on both Mobile

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim img1 As ImageView
Dim bmpImage As Bitmap
Dim pic As Intic=1
End Sub

Sub Activity_Create(FirstTime As Boolean)
'Create sd1 indesigner add just ImageView
Activity.LoadLayout("sd1")
bmpImage.Initialize(File.DirRootExternal,"/Mypic/" & "d9.jpg")
img1.Bitmap=bmpImage
End Sub
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
hi there try it like this

I have written this for myself to see if it works on my sony xperia 8. It is working fine. I have tested it on my other phone Star A3000 Duel sim also. works on both Mobile

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim img1 As ImageView
Dim bmpImage As Bitmap
Dim pic As Intic=1
End Sub

Sub Activity_Create(FirstTime As Boolean)
'Create sd1 indesigner add just ImageView
Activity.LoadLayout("sd1")
bmpImage.Initialize(File.DirRootExternal,"/Mypic/" & "d9.jpg")
img1.Bitmap=bmpImage
End Sub

This line is wrong. Looking at it I presume you meant something like
B4X:
Dim pic As Int : pic=1

The examples given above are to demonstrate using ContentChooser to select a file and displaying it, so as such your code is not a (full) replacement for it, although your code is fine for displaying a specific image :sign0098:
 
Upvote 0

magarcan

Active Member
Licensed User
Longtime User
I get the following error:
Error description: Unknown type: contentchooser
Are you missing a library reference?

Do I need any library??
 
Upvote 0
Top