将图像从相机保存到sqlite

时间:2012-02-07 03:01:31

标签: android sqlite android-camera

我有一个像这样的相机活动:

public void startCamera() 
{
    Log.d("ANDRO_CAMERA", "Starting camera on the phone...");
    String fileName = "testphoto.jpg";
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, fileName);
    values.put(MediaStore.Images.Media.DESCRIPTION,
            "Image capture by camera");
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
    imageUri = getContentResolver().insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
    startActivityForResult(intent, IMAGE_CAPTURE);
}

如何将此图像保存到SQLite数据库?


我有这样的代码:

fileName = helper.getKdStore(c) + "_" + System.currentTimeMillis() + ".jpg";
_path = Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera/" + fileName;

在启动相机中:

Cursor c = helper.getById(almagId);
c.moveToFirst();
File file = new File(_path, null);
try {
    file.createNewFile();
} catch (IOException e) {
    e.printStackTrace();
}
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, IMAGE_CAPTURE);
System.out.println(file);

Dbhelper helper = new Dbhelper(this);
SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL("update alfamapp set gambar ='"+file+"' where kdstore='"+helper.getKdStore(c)+"'");
db.close();
helper.close();

System.out.println(db);

我创建方法public void load()来显示我的图像:

detGam.setImageDrawable(Drawable.createFromPath(_path));

但我的应用程序无法显示图像。谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

将图像保存到sqlite是个坏主意,但是如果你还想继续下去,这里有代码

public static void insertPicture(byte[] data)
{

    SQLiteDatabase db = thisAct.openOrCreateDatabase(ConstantCodes.IMAGE_DATABASE, 2, null);
    ContentValues values = new ContentValues(1);
    values.put("photo", data);
    db.insert("photos", null, values);
    db.close();

}