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??
Try
Dim Obj1 As Reflector
Obj1.Target = Thebitmap
Obj1.RunMethod ("recycle")
Catch
log("Error Recycling Bitmap")
End Try
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
-call Activity.Finish in Activity_PauseB4X:Try Dim Obj1 As Reflector Obj1.Target = Thebitmap Obj1.RunMethod ("recycle") Catch log("Error Recycling Bitmap") End Try
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
B4X: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.
SetApplicationAttribute(android:largeHeap, "true")
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
its not supported on android 2.xB4X:SetApplicationAttribute(android:largeHeap, "true")
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
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 itHow do I load a web image through loadbitmapsample??
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
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
B4X: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
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)
I am not sure about how to use this code... like this??
B4X: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)
'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
1. Remove the reference to HttpUtils2 library.
2. Add HttpUtils2 modules instead.
3. Add this sub to HttpJob:
4. Modify ImageDownloader to use GetBitmapSample instead of GetBitmap.B4X:'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
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???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.
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???
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...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.
It will not happen. Images are only downloaded once.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???