Android Question java.lang.OutOfMemoryError

Mark Turney

Active Member
Licensed User
Longtime User
Hello B4A Community,

Thank you for a wonderful product and I can't wait to get into B4i after I complete my little app. I'm coming from the VB6 world, so this product is a God-send in creating mobile apps. I tried NSB App Studio, but there were so many limitations in the native access.

However, despite being able to figure out a lot on my own, I have been racking my brain for days searching the forum for a way to solve a memory issue. In a nutshell, what I am trying to do is read random images (previously entered into a SQLite db the user within this app), display them in an ImageView, then when the user clicks the ImageView, display the next randomly selected image. I have seen numerous discussions of using Reflections to recycle a bitmap, not reinitializing, etc. But, I must be missing something ... probably basic (pun intended) ;-/.

Here's the log:
java.lang.OutOfMemoryError
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:810)
at android.graphics.Bitmap.createBitmap(Bitmap.java:787)
at android.graphics.Bitmap.createBitmap(Bitmap.java:719)
at com.rootsoft.imageprocessing.RSImageProcessing.Rotate(RSImageProcessing.java:439)
at Face.Flashcards.main._readblob(main.java:1077)
at Face.Flashcards.main._rndrow(main.java:1116)
at Face.Flashcards.main._listview1_itemclick(main.java:966)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA$2.run(BA.java:285)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5135)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
at dalvik.system.NativeStart.main(Native Method)

And my code:
B4X:
Sub rndRow                                                                         'Random row for flashcards
    Dim totRows As Int
    Dim totRowsSelected As Int
    'Msgbox (listDate, "Test")                                                  'For testing
    totRows = SQL1.ExecQuerySingleResult("SELECT COUNT (*) FROM table1")
    totRowsSelected = SQL1.ExecQuerySingleResult("Select COUNT (*) from table1 where recordedDate >='" & listDate & "' order by recordedDate")
    selectedRndRow = Rnd(totRowsSelected, totRows)
    Msgbox ("Total Rows are: " & totRows & " and Tot Rows Selected is: " & totRowsSelected & " and Selected Random Row is: " & selectedRndRow, "Testing")   
    ReadBlob
End Sub

Sub ReadBlob
    'Using ExecQuery2 is safer as it escapes special characters automatically.
    'In this case it doesn't really matter.  
    Cursor1 = SQL1.ExecQuery("SELECT picture FROM table1 WHERE ROWID = '" & selectedRndRow & "'")
    Cursor1.Position = 0
    Dim Buffer() As Byte 'declare an empty byte array
    Buffer = Cursor1.GetBlob("picture")
    Dim InputStream1 As InputStream
    InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
    Dim ImageView4 As ImageView
    Dim Bitmap1 As Bitmap
    Bitmap1.Initialize2(InputStream1)
    InputStream1.Close   
    ImageView4.Initialize("")
    ImageView4.RemoveView
    panel2.AddView(ImageView4, 150, 60, panel2.Width-300, 990)
    ImageView4.Visible=True
    ImageView4.Gravity=Gravity.FILL
    'ImageView4.SetBackgroundImage(LoadBitmapSample(tmpDir, tmpFile, 800, 1200))
    ImageView4.SetBackgroundImage(Bitmap1)
    ImageView4.Bitmap = RSIE.rotate(Bitmap1, 90)
End Sub

Sub panel2_Click                    
    rndRow
End Sub

Thanks in advance for your assistance.
Mark
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Mark Turney

Active Member
Licensed User
Longtime User
Thanks for the reply Erel. I'll give your suggestions a try.

Mark
 
Upvote 0
Top