Android Question Downsampling image due to lack of memory

Discussion in 'Android Questions' started by catyinwong, Mar 4, 2015.

  1. catyinwong

    catyinwong Active Member Licensed User

    The error appears whenever I have switched between pages of the app for about 5-6 times.. I have searched the forum but there seems to be no solution to solve the problem??
     
  2. sorex

    sorex Expert Licensed User

    it depends on how you wrote your app and how it is working.

    without knowing that it's hard to give any advice.
     
  3. catyinwong

    catyinwong Active Member Licensed User

    It happens whenever it needs to download images... Like 3 to 4 in a module.. It seems likes the memory doesn't release even if I finish the activity?
     
  4. KZero

    KZero Active Member Licensed User

    how you load your bitmaps ? post the relevant part of code if possible
    -try using LoadBitmapSample if you loading high res. images
    -try recycling the bitmap if its not in use
    Code:
    Try
        
    Dim Obj1 As Reflector
        Obj1.Target = Thebitmap
        Obj1.RunMethod (
    "recycle")
        
    Catch
        
    log("Error Recycling Bitmap")
        
    End Try
    -call Activity.Finish in Activity_Pause
     
  5. catyinwong

    catyinwong Active Member Licensed User


    Code:
    For inx = 0 To m.size - 1  'm is the map where I put all the picture address
     Dim picv As String = m.Get(inx) 
     p.Initialize(
    ""
     clv.add(p,
    100%x + 5,link) 'clv is the custom list view
     p.LoadLayout("ImageView")
     
    If picv <> Null Then m2.Put(imageview1,picv) 'I use the image downloader to put the image into the imageview
     Next
    Where should I put the codes for recycling the bitmap???
    I try calling activity.finish in Activity_pause, but it didn't help. The memory used up as fast as before.
     
  6. KZero

    KZero Active Member Licensed User

    what the bitmap resolutions and how many bitmaps loaded
    you can recycle the bitmap if its not used anymore but i think u always using it

    -try to load the bitmaps 1 time and store them in process global variables instead of reloading each time the activity start

    if you loading large bitmaps or high number try adding this line to your manifest editor
    Code:
    SetApplicationAttribute(android:largeHeap, "true")
    its not supported on android 2.x
     
  7. catyinwong

    catyinwong Active Member Licensed User

    Some examples:
    http://static.wixstatic.com/media/4...ng_srz_p_519_460_75_22_0.50_1.20_0.00_png_srz
    http://static.wixstatic.com/media/4...pg_srz_p_252_252_75_22_0.50_1.20_0.00_jpg_srz
    Not sure about the resolutions

    The major problem I think is that the memory doesn't clear up after the close the module (end the activity) because at first the pictures are loaded fine.. I try to close and open and close and open the module, then the memory is used up for about 5-6 times.. Actually they should not store anything after I close them.. Otherwise the problem will never be solved and the memory will eventually used up anyway.. How to clear the memory after the module is close??
     
  8. KZero

    KZero Active Member Licensed User

    the system will automatically release the activity memory if its not in use but its not immediately.
    the images are big if you loading them into small imageviews in the CLV you should use LoadBitmapSample it will help to decrease the memory usage
    if this images is frequently used in your application i recommend to load them one time and store them in process global variables to stop memory increasing each time you load them
     
  9. catyinwong

    catyinwong Active Member Licensed User

    How do I load a web image through loadbitmapsample??
     
  10. KZero

    KZero Active Member Licensed User

    by saving the bitmap to file (i think you should save it to DirInternalCache instead of re-downloading each time) or try this code to scale the bitmap without saving it

    Code:
    Sub CreateScaledBitmap(Original As Bitmap, Width As Int, Height As Int) As Bitmap
        
    Dim r As Reflector
        
    Dim Filter As Boolean =False
        
    Dim b As Bitmap
        b = r.RunStaticMethod(
    "android.graphics.Bitmap""createScaledBitmap", _
            
    Array As Object(Original, Width, Height, Filter), _
            
    Array As String("android.graphics.Bitmap""java.lang.int""java.lang.int""java.lang.boolean"))
        
    Return b
    End Sub
     
  11. catyinwong

    catyinwong Active Member Licensed User

    I am not sure about how to use this code... like this??

    Code:
    Dim pic as string 'as the picture address
    Dim Imageview1 as imageview 'as the image view to show the picture
    Imageview1.bitmap = CreateScaledBitmap(pic,Imageview1.width,Imageview1.height)
     
  12. KZero

    KZero Active Member Licensed User

    this function to resize the Bitmap not to download it so you need to pass the downloaded Bitmap not the picture address

    i'm not familiar with the used Image downloader and how to get the Bitmap using it
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    1. Remove the reference to HttpUtils2 library.
    2. Add HttpUtils2 modules instead.
    3. Add this sub to HttpJob:
    Code:
    'Returns the response as a bitmap loaded with LoadBitmapSample.
    Public Sub GetBitmapSample(Width As Int, Height As Int) As Bitmap
       
    Return LoadBitmapSample(HttpUtils2Service.TempFolder, taskId, Width, Height)
    End Sub
    4. Modify ImageDownloader to use GetBitmapSample instead of GetBitmap.
     
    Ju Yang and KZero like this.
  14. catyinwong

    catyinwong Active Member Licensed User

    In the ImageDownloader_GetBitmapSample, what should I put as the width and height parameter?? Shouldn't it be flexible according to the imageview or the web image??
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    You will need to implement such a solution. You will need to find the correct ImageView and get its size. However you can also use an approximated size that will be good for all images.
     
  16. catyinwong

    catyinwong Active Member Licensed User

    How should I modify the ImageDownloader to determine whether the same images have already been downloaded and reload the previously downloaded image so that the downloading won't repeat and waste the memory???
     
  17. Troberg

    Troberg Well-Known Member Licensed User

    You'll have to create some kind of mechanism to remember what images you have downloaded. There isn't anyone made for you. It's not that hard to do, so just give it a try.
     
  18. catyinwong

    catyinwong Active Member Licensed User

    Let me put it this way.. For the ImageDownloader, anyone knows where the images are stored?? Is it in a temp file, a map or a list form?? Because I need to figure out how it works before I can try to modify it...
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    It will not happen. Images are only downloaded once.
     
  20. sanduro

    sanduro Member Licensed User

    I ve had a same problem and aetting large heap parameteter hellped me

    San
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice