Android Question Assign memory, resize images.

WebQuest

Active Member
Licensed User
I'm having trouble resizing an image captured by the cell phone camera. The problem arises when I try to load the image in an ImageView with the result of this error: java.lang.OutOfMemoryError: Failed to allocate a 8294412 byte allocation with 7326352 free bytes and 6MB until OOM.


Obviously the image is too big, I tried to resize the image before it was loaded in the listview without success. does anyone know how to solve? this is the code I'm using:
B4X:
Sub Camera1_PictureTaken (Data() As Byte)

    Dim filename As String = "Camera1.jpg"
    Dim dir As String = File.DirDefaultExternal
    camEx.SavePictureToFile(Data,dir, filename)
   
   
    tempBitmap=LoadBitmap(dir,filename)
   
   

    StartActivity("ImageHomeImage")
   
   
   
    LoadResult
End Sub

Sub CreateScaledBitmap2(Original As Bitmap, NewWidth As Int, NewHeight As Int) As Bitmap
    Dim r As Reflector
    tempBitmap= r.RunStaticMethod("android.graphics.Bitmap", "createScaledBitmap", _
        Array As Object(Original, NewWidth, NewHeight, True), _
        Array As String("android.graphics.Bitmap", "java.lang.int", "java.lang.int", "java.lang.boolean"))
    Return tempBitmap
End Sub
Sub LoadResult
    tempBitmap=CreateScaledBitmap2(tempBitmap,1920,1080)
    ImageHomeImage.CameraTemp= tempBitmap
End Sub
 

BillMeyer

Well-Known Member
Licensed User
Longtime User
Try putting this in your Manifest First
B4X:
SetApplicationAttribute(android:largeHeap,"true")

Then secondly, you are not making much of a difference with a 1920 x 1080 image. I would guess that a 640 x 480 would suffice - Try it and report back.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
try if u can use LoadBitmapResize and less temp variables.
at least u should null this public / global variables after use.
 
Upvote 0

WebQuest

Active Member
Licensed User
Thanks for the advice I solved with: SetApplicationAttribute (android: largeHeap, "true"). The only problem that when I take the picture and upload it in the listview is rotated, and I do not understand why?
 
Upvote 0

BillMeyer

Well-Known Member
Licensed User
Longtime User
Last edited:
Upvote 0

WebQuest

Active Member
Licensed User
Hi Billmeyer thanks for the reply. I use a Samsung and Camera Lib because I only need the camera to capture images taken. What should I do to get the images correctly displayed in the imageView?

I am using this code to load the image saved in an ImageView but the rotated charge and with wrong proportions.
B4X:
Sub CameraLoad

        CameraTemp=CreateScaledBitmap(CameraTemp,640,480)
      ImageViewResult.SetBackgroundImage(CameraTemp)
        
        
    
End Sub
 
Upvote 0
Top