Android Question java.lang.OutOfMemoryError

Itila Tumer

Active Member
Licensed User
Longtime User
Hello there,

I am getting error when updating photos.


B4X:
#Region  Activity Attributes
    #FullScreen: True
    #IncludeTitle: False
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

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.
    
    Private edtFirstName As EditText
    Private edtLastName As EditText
    'Private spnCinsiyet As Spinner
    Private edtBirthDay As EditText
    Private edtBirthCity As EditText
    Private edtLocation As EditText
    Private edtFatherName As EditText
    Private edtEngel As EditText
    Private edtKimlik As EditText
    Private btnFirst As Button
    Private btnPrevious As Button
    Private btnNext As Button
    Private btnLast As Button
    Private btnAdd As Button
    Private btnUpdate As Button
    Private btnDelete As Button
    Private edtCinsiyet As EditText
    Private IbDelete As Label
    Private IbUpdate As Label
    Private spinnerMap As Map
    Private IbAdd As Label
    
    
    'FOTOĞRAF
    
    Dim Panel1 As Panel
    Dim camera1 As Camera
    Dim btnTakePicture As Button
    Private btnAra As Button
    Dim  chooser As ContentChooser
    Private ImageView1 As ImageView
    Public flag As String
    Public Buffer() As Byte
    Private spnCinsiyet As Spinner
    Private edtGrup_1 As EditText
    Private edtGrup_2 As EditText
    Private edtGrup_3 As EditText

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.LoadLayout("DenekEkle")
End Sub

Sub Activity_Resume
    
    If Starter.CurrentIndex > -1 Then
        ShowEntry(Starter.CurrentIndex)                'show the first entry
    End If
    
    camera1.Initialize(Panel1, "Camera1")
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    camera1.Release
End Sub


Sub Camera1_Ready (Success As Boolean)
    If Success Then
        camera1.StartPreview
        btnTakePicture.Enabled = True
    Else
        ToastMessageShow("Cannot open camera.", True)
    End If
End Sub

Sub Camera1_PictureTaken (Data() As Byte)
    
    Dim Query As String
    Dim Cursor1 As Cursor
    Dim ID As Int
    
    If Starter.EditMode="Add" Then
        ID = Starter.SQL1.ExecQuerySingleResult("SELECT max(ID) FROM Denek")
        ID=ID+1
    End If
    If Starter.EditMode="Edit" Then
        ID = Starter.IDList.Get(Starter.CurrentIndex)
    End If
        
    Dim filename As String = ID&".jpg"
    Dim dir As String = File.DirRootExternal
    camera1.StartPreview
    Dim out As OutputStream
    out = File.OpenOutput(dir,filename, False)
    out.WriteBytes(Data, 0, Data.Length)
    out.Close
    Dim i As Intent
    i.Initialize("android.intent.action.MEDIA_SCANNER_SCAN_FILE", _
"file://" & File.Combine(dir,filename))
    Dim p As Phone
    p.SendBroadcastIntent(i)
End Sub

Sub btnTakePicture_Click
    btnTakePicture.Enabled = False
    camera1.TakePicture
End Sub

Sub btnAra_Click
    'camEx.Release
    chooser.Initialize("Chooser")
    chooser.Show("image/*", "Choose image")
End Sub

Sub Chooser_Result (Success As Boolean, Dir As String, FileName As String)
    ' Dim Query As String

    
    If Success Then

        'display the chosen image in ImageView
        Dim bmp As Bitmap
        bmp.Initialize(Dir, FileName)
        ImageView1.Bitmap = bmp
    
        'Ask user if they want to save the image to DB ..
        
        'convert the image file to a bytes array
        'flagSaveImage = True
        Dim ADD As String
        Dim UP As String
        flag=ADD
        flag=UP
        Dim InputStream1 As InputStream
        InputStream1 = File.OpenInput(Dir, FileName)
    
        Dim OutputStream1 As OutputStream
        OutputStream1.InitializeToBytesArray(1000)
    
        File.Copy2(InputStream1, OutputStream1)
    
        '    Dim Buffer() As Byte 'declares an empty array
        Buffer = OutputStream1.ToBytesArray
            
            
    End If
    
End Sub

Sub ShowEntry(EntryIndex As Int)
    Private Cursor1 As Cursor
    Private ID As Int
    spinnerMap.Initialize
    If Starter.IDList.Size = 0 Then             'check if database is empty
        Return                                                    'if the database is empty leave the routine
    End If
    
    If Starter.EditMode = "Add" Then
        
        edtFirstName.Text = ""
        edtLastName.Text = ""
        edtBirthDay.Text = ""
        edtBirthCity.Text = ""
        edtLocation.Text = ""
        edtEngel.Text= ""
        edtKimlik.Text = ""
        spnCinsiyet.Prompt = "Cinsiyet" '"Select One Option"
        'spinnerMap.Put("", "")
        spnCinsiyet.ADD("Bay")
        spinnerMap.Put("Bay", "id 2")
        spnCinsiyet.ADD("Bayan")
        spinnerMap.Put("Bayan", "id 3")       
        edtFatherName.Text = ""
        edtGrup_1.Text = ""
        edtGrup_2.Text = ""
        edtGrup_3.Text = ""
    
    Else
        ID = Starter.IDList.Get(EntryIndex)        'get the ID for the given entry index
        'read the entry with the given ID
        Cursor1 = Starter.SQL1.ExecQuery("SELECT * FROM Denek WHERE ID = " & ID)
        Cursor1.Position = 0                                                                    'set the cursor
        edtFirstName.Text = Cursor1.GetString("Isim")        'read the value of the FirstName column
        edtLastName.Text = Cursor1.GetString("Soyisim")            'read the value of the LasstName column
        edtBirthDay.Text = Cursor1.GetString("DogumTarihi")                    'read the value of the City column
        edtBirthCity.Text = Cursor1.GetString("DogumYeri")
        edtLocation.Text = Cursor1.GetString("YasadiğiSehir")
        edtEngel.Text= Cursor1.GetString("EndelDurumu")
        edtKimlik.Text = Cursor1.GetString("TcNo")
        spnCinsiyet.Prompt = "Cinsiyet"
        spnCinsiyet.ADD(Cursor1.GetString("Cinsiyet"))
        
        If Cursor1.GetString("Cinsiyet")= ("Bay") Then
            spnCinsiyet.ADD("Bayan")
            spinnerMap.Put("Bayan", "id 3")
        Else
            spnCinsiyet.ADD("Bay")
            spinnerMap.Put("Bay", "id 2")
        End If
        
        Dim IpSt As InputStream
        Dim Bitmap1 As Bitmap
        Buffer = Cursor1.GetBlob("Resim")
        IpSt.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
        Bitmap1.Initialize2(IpSt)
        IpSt.Close
        ImageView1.SetBackgroundImage(Bitmap1)
        Try

        Catch
            Log(LastException)

            Msgbox ("Resim Mevcut Değil!","Dikkat")
        End Try
        edtFatherName.Text = Cursor1.GetString("BabaAdi")
        edtGrup_1.Text = Cursor1.GetString("Group1")
        edtGrup_2.Text = Cursor1.GetString("Group2")
        edtGrup_3.Text = Cursor1.GetString("Group3")
        
        
        
        
        
        Cursor1.Close                                                                                    'close the cursor, we don't it anymore
    

    End If
    ShowButtons
        

    
End Sub
Sub AddEntry
    Dim Query As String
    Dim Cursor1 As Cursor
'    Dim Cursor2 As Cursor
    Dim ID As Int
    
        
    'first we check if the entry already does exist   DİKKAT => BİRÇOK kez  AND koymayı unuttum aralarına
    Query = "SELECT * FROM Denek WHERE Isim = ? AND Soyisim = ? AND DogumTarihi = ? AND DogumYeri = ? AND YasadiğiSehir = ? AND EndelDurumu = ? AND TcNo = ? AND Cinsiyet = ? AND Resim = ? AND BabaAdi = ? AND Group1=? AND Group2 = ? AND Group3 = ?"
    Cursor1 = Starter.SQL1.ExecQuery2(Query, Array As String (edtFirstName.Text, edtLastName.Text, edtBirthDay.Text, edtBirthCity.Text, edtLocation.Text, edtEngel.Text, edtKimlik.Text, spnCinsiyet.SelectedItem,edtFatherName.Text, edtGrup_1.Text,edtGrup_2.Text,edtGrup_3.Text ))
    
    If Cursor1.RowCount > 0 Then
        'if it exists show a message and do nothing else
        ToastMessageShow("This entry already exists", False)
    Else
        'if not, add the entry
        'a NULL for the ID column increments the primary key automatically by one
        'we use ExecNonQuery2 because it's easier, we don't need to take care of the data types
        
        
        
            Query = "INSERT INTO Denek VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)"
            Starter.SQL1.ExecNonQuery2(Query, Array As Object(edtFirstName.Text, edtLastName.Text, edtBirthDay.Text, edtBirthCity.Text, edtLocation.Text, edtEngel.Text, edtKimlik.Text, spnCinsiyet.SelectedItem,Buffer,edtFatherName.Text, edtGrup_1.Text,edtGrup_2.Text,edtGrup_3.Text ))
            'Activity2.SQL1.ExecNonQuery2(Query, Array As Object(Buffer))
            

        
        
            'to display the ID of the last entry we read the max value of the ID column
            ID = Starter.SQL1.ExecQuerySingleResult("SELECT max(ID) FROM Denek")
            Starter.RowNumber = Starter.RowNumber + 1                    'increase the row count
            Starter.IDList.ADD(ID)                                        'add the last ID to the list
            Starter.CurrentIndex = Starter.IDList.Size - 1        'set the current index to the last one
                    
        
    
        
        '    Activity2.SQL1.ExecNonQuery2("INSERT INTO persons VALUES( NULL, NULL,NULL, NULL,NULL, NULL, NULL, 'Resim')", Array As Object(Buffer))
    
        '    Buffer = Null  ' not tested ... blank buffer contents and reset image flag
        ' flagSaveImage = False
    
        
    End If
    Cursor1.Close
    
    ShowButtons
End Sub
Sub DeleteEntry
    Private Query As String
    Private Answ As Int
    
    'ask the user for confirmation
    Answ = Msgbox2("Do you really want to delete " & edtFirstName.Text & " " & edtLastName.Text, "Delete entry", "Yes", "", "No", Null)

    If Answ = DialogResponse.POSITIVE Then            'if yes, delete the entry
        Query = "DELETE FROM Denek WHERE ID = " & Starter.IDList.Get(Starter.CurrentIndex)
        Starter.SQL1.ExecNonQuery(Query)                                    'delete the entry
        Starter.IDList.RemoveAt(Starter.CurrentIndex)                            'remove the ID from the list
        If Starter.CurrentIndex = Starter.RowNumber - 1 Then            'if the current index is the last one
            Starter.CurrentIndex = Starter.CurrentIndex - 1                    'decrement it by 1
        End If
        Starter.RowNumber = Starter.RowNumber - 1                                    'decrement the row count by 1
        ShowEntry(Starter.CurrentIndex)                                        'show the next entry
        ToastMessageShow("Entry deleted", False)    'confirmation for the user
        ShowButtons
    End If
End Sub

Sub UpdateEntry
    Private Query As String
    
    Query = "UPDATE Denek Set Isim = ?, Soyisim = ?,  DogumTarihi = ?, DogumYeri = ?, YasadiğiSehir = ?, EndelDurumu = ?, TcNo = ?, Cinsiyet = ?,Resim = ?, BabaAdi = ?, Group1 = ?, Group2 = ?, Group3 = ? WHERE ID = " & Starter.IDList.Get(Starter.CurrentIndex)
    Starter.SQL1.ExecNonQuery2(Query, Array As Object(edtFirstName.Text, edtLastName.Text, edtBirthDay.Text , edtBirthCity.Text, edtLocation.Text, edtEngel.Text, edtKimlik.Text, spnCinsiyet.SelectedItem, Buffer,  edtFatherName.Text,edtGrup_1.Text, edtGrup_2.Text,edtGrup_3.Text))
    'Starter.SQL1.ExecNonQuery2(Query, Array As Object(edtFirstName.Text, edtLastName.Text, spnCinsiyet.SelectedItem, edtBirthDay.Text, edtBirthCity.Text, edtFatherName.Text,Buffer,edtEngel.text))
    'Else
    ToastMessageShow("Entry updated", False)
End Sub


Sub ShowButtons
    If Starter.EditMode = "Edit" Then
        btnPrevious.Visible = False        'hide the navigation buttons
        btnNext.Visible = False
        btnFirst.Visible = False
        btnLast.Visible = False
        btnAdd.Visible = False
        IbAdd.Visible = False
            
        
    Else
        
        
        IbUpdate.Visible = False
        btnUpdate.Visible = False
        btnDelete.Visible = False
        IbDelete.Visible = False
        
            
        If Starter.RowNumber <= 1 Then         'check if the database has less than 2 empty
            btnPrevious.Visible = False        'hide the navigation buttons
            btnNext.Visible = False
            btnFirst.Visible = False
            btnLast.Visible = False
        Else
            'show or hide the Previous and Next buttons depending on the value of the current index
            If Starter.CurrentIndex = 0 Then
                btnPrevious.Visible = False
                btnFirst.Visible = False
            Else
                btnPrevious.Visible = True
                btnFirst.Visible = True
            End If
            
            If Starter.CurrentIndex = Starter.IDList.Size - 1 Then
                btnNext.Visible = False
                btnLast.Visible = False
            Else
                btnNext.Visible = True
                btnLast.Visible = True
            End If
        End If
    End If
    
End Sub



Sub btnDelete_Click
    DeleteEntry
End Sub

Sub btnAdd_Click
    AddEntry
    Starter.EditMode = "Edit"
    ShowButtons
End Sub

Sub btnUpdate_Click
    UpdateEntry
End Sub


Sub btnPrevious_Click
    If Starter.CurrentIndex > 0 Then
        Starter.CurrentIndex = Starter.CurrentIndex - 1
        ShowEntry(Starter.CurrentIndex)
    End If
End Sub

Sub btnNext_Click
    If Starter.CurrentIndex < Starter.RowNumber - 1 Then
        Starter.CurrentIndex = Starter.CurrentIndex + 1
        ShowEntry(Starter.CurrentIndex)
    End If
End Sub

Sub btnFirst_Click
    Starter.CurrentIndex = 0
    ShowEntry(Starter.CurrentIndex)
End Sub

Sub btnLast_Click
    Starter.CurrentIndex = Starter.IDList.Size - 1
    ShowEntry(Starter.CurrentIndex)
End Sub








'*************************************



Sub edtFirstName_FocusChanged (HasFocus As Boolean)
    If HasFocus = True Then
        DoEvents
        edtFirstName.SelectAll
        DoEvents
        edtFirstName.SelectAll
    End If
End Sub

Sub edtLastName_FocusChanged (HasFocus As Boolean)
    If HasFocus = True Then
        DoEvents
        edtLastName.SelectAll
        DoEvents
        edtLastName.SelectAll
    End If
End Sub

Sub edtKimlik_FocusChanged (HasFocus As Boolean)
    If HasFocus = True Then
        DoEvents
        edtKimlik.SelectAll
        DoEvents
        edtKimlik.SelectAll
    End If
End Sub


Sub edtCinsiyet_FocusChanged (HasFocus As Boolean)
    If HasFocus = True Then
        DoEvents
        edtCinsiyet.SelectAll
        DoEvents
        edtCinsiyet.SelectAll
    End If
End Sub
 

Attachments

  • error.png
    error.png
    27.4 KB · Views: 235

Itila Tumer

Active Member
Licensed User
Longtime User
java.lang.OutOfMemoryError
at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:91)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:201)
at anywheresoftware.b4a.objects.streams.File.Copy2(File.java:362)
at b4a.example.denekekle._chooser_result(denekekle.java:1153)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:733)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:352)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at anywheresoftware.b4a.phone.Phone$ContentChooser$1.ResultArrived(Phone.java:865)
at anywheresoftware.b4a.BA$4.run(BA.java:563)
at anywheresoftware.b4a.BA.setActivityPaused(BA.java:437)
at b4a.example.denekekle$ResumeMessage.run(denekekle.java:306)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5306)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
 
Upvote 0

Itila Tumer

Active Member
Licensed User
Longtime User
Error line does not show.

Would like to change the current photo
Gives the error when I choose the new photo from gallery
I do my photography operation here.

B4X:
Sub btnAra_Click
    'camEx.Release
    chooser.Initialize("Chooser")
    chooser.Show("image/*", "Choose image")
End Sub

Sub Chooser_Result (Success As Boolean, Dir As String, FileName As String)
    ' Dim Query As String

    
    If Success Then

        'display the chosen image in ImageView
        Dim bmp As Bitmap
        bmp.Initialize(Dir, FileName)
        ImageView1.Bitmap = bmp
    
        'Ask user if they want to save the image to DB ..
        
        'convert the image file to a bytes array
        'flagSaveImage = True
        Dim ADD As String
        Dim UP As String
        flag=ADD
        flag=UP
        Dim InputStream1 As InputStream
        InputStream1 = File.OpenInput(Dir, FileName)
    
        Dim OutputStream1 As OutputStream
        OutputStream1.InitializeToBytesArray(1000)
    
        File.Copy2(InputStream1, OutputStream1)
    
        '    Dim Buffer() As Byte 'declares an empty array
        Buffer = OutputStream1.ToBytesArray
            
            
    End If
    
End Sub
 
Upvote 0

eps

Expert
Licensed User
Longtime User
My aim is to take photos and show them from gallery.

I'm gonna start researching.
thanks

Take a look at Ultimate List View - this has been discussed a lot. Images are memory hungry and you need to approach them with a certain amount of care and consideration. If they are physically big (not necessarily big in terms of disk storage) then the device will struggle as it unfolds the image.

ULV is here https://www.b4x.com/android/forum/threads/lib-chargeable-ultimatelistview.22736/
 
Upvote 0

Itila Tumer

Active Member
Licensed User
Longtime User
B4X:
        ImageView1.Bitmap = LoadBitmapResize(Dir, FileName, ImageView1.Width, ImageView1.Height, True)

I've tried.

I got an error.

java.lang.OutOfMemoryError
at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:91)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:201)
at anywheresoftware.b4a.objects.streams.File.Copy2(File.java:362)
at b4a.example.denekekle._chooser_result(denekekle.java:1153)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:733)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:352)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at anywheresoftware.b4a.phone.Phone$ContentChooser$1.ResultArrived(Phone.java:865)
at anywheresoftware.b4a.BA$4.run(BA.java:563)
at anywheresoftware.b4a.BA.setActivityPaused(BA.java:437)
at b4a.example.denekekle$ResumeMessage.run(denekekle.java:306)
at android.os.Handler.handleCallback(Handler.java:725)
 
Upvote 0
Top