views:

93

answers:

2

Greetings yet again. I have an minor issue when taking a photo. I have a button that invokes the camera, successfully takes a photo, and returns to my entry form. My only problem is the database leak that occurs when pressing the button to call the camera. My code looks a little something like this.

public void takephoto(){

        Intent in = new
        Intent(this, takephoto.class);
        startActivityForResult(in,1);
    }

Here's a snippet of how the database is opened:

 public journalDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }

I run :

mDbHelper = new journalDbAdapter(this);
        mDbHelper.open();

In the onCreate of the Activity or Class that I am calling the camera in. I have not coded in mDbHelper.close(); anywhere not sure if i should when or where. I think i would rather just leave it open while capturing one image.

Logcat:

08-17 21:33:37.582: ERROR/Database(18297): java.lang.IllegalStateException: /data/data/com.growjournal.beta/databases/grower SQLiteDatabase created and never closed
08-17 21:33:37.582: ERROR/Database(18297):     at android.database.sqlite.SQLiteDatabase.(SQLiteDatabase.java:1785)

What possible problems could I face if this was never fixed??

Everything seems to be working fine, but i would defiantly like to avoid any issues amongst the many android devices out there.

A: 

I think you are missing this line

mDb.close();
Rahul
should i close when taking the photo? will the activity restart and reopen the database when returning?
Brian
no after inserting records into the database you should close it
Rahul
still not sure where to put it, should it be in my database adapter or in the activity where i update the fields. I tried placing it in several places but began getting force closes. really i need it open for everything in the app as every screen is either pulling a list or editing/creating records. except the camera
Brian
I am not able to understand your code properly. But if you just have to capture an image and store it in your application's database check this program. In the onActivityResult they would have started a method which will return a bitmap that needs to be stored as blob in the database. You can open your db at this point and close it once you are donehttp://labs.makemachine.net/wp-content/uploads/2010/03/2010_04_09_make_machine_photo_capture.zip
Rahul
I'm not directly saving the image to a database; but rather in the process of editing or creating a records(filling out a form). There is a button to take and save a photo. I do not have a leak, according to log cat, until i call the camera. I'm assuming that java thinks i'm done with the database when i'm not
Brian
A: 

You're getting this error because the camera app is a huge memory hog and is most likely causing your app to close instead of just pausing. You should save your records and close the database in onPause and reopen it in onResume and/or onCreate.

aschmack
I'm not using a separate camera app. I have my own activity using the camera and saving to a directory that i have specified.
Brian