gpt4 book ai didi

android - 如何将android数据库中的照片保存为blob

转载 作者:行者123 更新时间:2023-11-30 03:32:35 27 4
gpt4 key购买 nike

我正在尝试将照片保存为 SQLite 中的 blob(而不仅仅是引用它)。 mCurrentMediaPath 是存储照片的当前路径。现在,我需要在拍摄照片并按下保存按钮后将其保存到数据库中(我猜是在 Intent 之后)。

public Uri insert(byte[] image) {
return getContentResolver().insert(MyContentProvider.CONTENT_URI7, createContentValues(image));
}
private ContentValues createContentValues(byte[] image) {
ContentValues docsInsert = new ContentValues();
docsInsert.put(Db.COLUMN_FILETYPE, "PHOTO");
docsInsert.put(Db.COLUMN_NAME, mCurrentMediaPath);
docsInsert.put(Db.COLUMN_FILE, image);
return docsInsert;
}
// convert from bitmap to byte array
public byte[] getBytesFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 70, stream);
return stream.toByteArray();
}


private void dispatchMediaIntent(int actionCode) {
switch(actionCode) {
case ACTION_TAKE_PHOTO:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = null;
try {
f = setUpPhotoFile(ACTION_TAKE_PHOTO);
mCurrentMediaPath = f.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
} catch (IOException e) {
e.printStackTrace();
f = null;
mCurrentMediaPath = null;
}
startActivityForResult(takePictureIntent, actionCode);
break;
case ACTION_TAKE_VIDEO:
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(takeVideoIntent, actionCode);
break;
default:
break;
}
}

我应该在哪里实现插入?

     //SAVING TO DATABASE
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentMediaPath, bmOptions);
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentMediaPath, bmOptions);

insert(getBytesFromBitmap(bitmap));

最佳答案

然后创建图像的位图

Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, null);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();

dba.open();

dba.insertPhoto(byteArray);

其中 dba 是数据库类的对象。

在数据库类中创建表,如:

private static final String CREATETABLE_PHOTO = "create table eqpphoto("EImage BLOB " + ");";

public static final String TABLE_PHOTO = "eqpphoto";

public long insertPhoto(byte[] EImage) {

try {
System.out.println("Function call : ");
ContentValues values = new ContentValues();

values.put(EIMAGE, EImage);
return db.insert(TABLE_PHOTO, null, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}

关于android - 如何将android数据库中的照片保存为blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17191097/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com