save image from camera into sqlite

淺唱寂寞╮ 提交于 2020-01-06 09:04:03

问题


I have an camera Activity like this:

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);
}

What should I do to save this image to a SQLite database?


I have code like this:

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

In start camera:

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);

and I create method public void load() to display my image:

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

but my application can't display the image. Thank you for your help.


回答1:


Saving Image in to sqlite is bad idea, however if you still want to go further, there here is code

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();

} 


来源:https://stackoverflow.com/questions/9170446/save-image-from-camera-into-sqlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!