1

I am trying to save images from an Android phone in a SQLite DB. Here is the code:

Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera c) {
            currentPictureName = Utils.getUserName(getApplicationContext()) 
                    + System.currentTimeMillis() + ".jpg";

            DBHelper helper = new DBHelper(getApplicationContext());
            SQLiteDatabase db = helper.getReadableDatabase();
            ContentValues values = new ContentValues();
            values.put("name", currentPictureName);
            values.put("image", data);
            //db.insert(DBHelper.TABLE_NAME, null, values);
            DBHelper.insertDB(db, DBHelper.TABLE_NAME, null, values);

            mCamera.startPreview();
        }
    };

DBHelper.java

package com.apps.terrapin;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {
    private static Context mContext;
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "terrapin";
    public static final String TABLE_NAME = "images";
    private static final String TABLE_CREATE =
                "CREATE TABLE " + TABLE_NAME + " (" +
                "_id INTEGER PRIMARY KEY, name TEXT, image BLOB);";

    DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        mContext = context;
    }

    public static void insertDB(final SQLiteDatabase db, String tableName, String hack, final ContentValues values) {
        Log.e("TAG", "Insert DB");
        new Thread(new Runnable() {
            public void run() {
                db.insert(DBHelper.TABLE_NAME, null, values);
            }
        }).start();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.e("TAG", "Creating DB");
        db.execSQL(TABLE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion >= newVersion)
            return;

        onCreate(db);       
    }
}

The problem is, my app saves images properly a few times and then when I click on the save image button, it hangs. Then Android asks me to force close it. I had this problem with the normal insert method so I thought putting the insert in a thread might help. But it did not. What is wrong here? Logcat does not show any errors, there is one line which says

    12-13 05:02:08.005: I/InputDispatcher(93): Application is not responding: 
Window{406d17f8 com.apps.terrapin/com.apps.terrapin.TerraPin paused=false}.  
10013.7ms since event, 10012.2ms since wait started

I am using API level 10

ACC
  • 2,488
  • 6
  • 35
  • 61
  • you need to store the image as a BLOB in android, – Abhi Dec 13 '11 at 10:14
  • Yes, I defined the image column as a blob. Isn't the function call correct? – ACC Dec 13 '11 at 10:18
  • 1
    did you miss the `start()` on the new thread in `insertDB()`? – Karthik Dec 13 '11 at 10:23
  • Oh yes, I did miss `start()`. Added it now but still got the same problem. – ACC Dec 13 '11 at 10:36
  • I thought it might be a good idea to save images in a DB since all phones might not have SD cards (the phone I am testing on does not have one, it's a HTC Evo) – ACC Dec 13 '11 at 10:39
  • since you are using a Thread, the ANR will not be caused by insert. Can you add some more Logs and find out at what point the code execution (in UI thread) is waiting for long time. – Karthik Dec 13 '11 at 11:45
  • http://stackoverflow.com/questions/7331310/how-to-store-image-as-blob-in-sqlite-how-to-retrive-it see this link – Abhi Dec 13 '11 at 11:56

1 Answers1

1

Carefull, your db is basically in your internal memory, while it's easy to save picture in external storage. Doing this will increase the size of your app... Why don't save in your db the path to the file, and save it out the internal storage?

castrogne
  • 521
  • 1
  • 4
  • 17
  • 2
    What you have given as answer is actually not an answer. It should have been added as a comment. Secondly, there is NO restriction to put database file in internal memory. Many apps put database in sdcard. – Sarwar Erfan Dec 13 '11 at 10:35
  • You're right, I should have added a comment. But I'm new in Stackoverflow and don't have enough credentials to add comments. I only can add comment to my own answers. – castrogne Dec 15 '11 at 10:52