如何保存在android系统数据库中的照片作为一个blob [英] How to save a photo in android database as a blob
本文介绍了如何保存在android系统数据库中的照片作为一个blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想保存的照片作为SQLite的(不只是引用它)的BLOB。 mCurrentMediaPath是当前所在的路径照片将被保存。现在,我需要拍摄照片后,将其保存到数据库中,并保存按钮pressed(我猜的意图后)。
公众开放的插入(字节[]图像){
返回getContentResolver()插入(MyContentProvider.CONTENT_URI7,createContentValues(图片))。
}
私人ContentValues createContentValues(字节[]图像){
ContentValues docsInsert =新ContentValues();
docsInsert.put(Db.COLUMN_FILETYPE,PHOTO);
docsInsert.put(Db.COLUMN_NAME,mCurrentMediaPath);
docsInsert.put(Db.COLUMN_FILE,图像);
返回docsInsert;
}
//从位图转换为字节数组
公众的byte [] getBytesFromBitmap(位图位图){
ByteArrayOutputStream流=新ByteArrayOutputStream();
bitmap.com preSS(比较pressFormat.JPEG,70,流);
返回stream.toByteArray();
}
私人无效dispatchMediaIntent(INT行动code){
开关(动作code){
案例ACTION_TAKE_PHOTO:
意图takePictureIntent =新意图(MediaStore.ACTION_IMAGE_CAPTURE);
文件F = NULL;
尝试{
F = setUpPhotoFile(ACTION_TAKE_PHOTO);
mCurrentMediaPath = f.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(F));
}赶上(IOException异常五){
e.printStackTrace();
F = NULL;
mCurrentMediaPath = NULL;
}
startActivityForResult(takePictureIntent,动作code);
打破;
案例ACTION_TAKE_VIDEO:
意图takeVideoIntent =新意图(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(takeVideoIntent,动作code);
打破;
默认:
打破;
}
}
我应该
在哪里执行插入?
//保存到数据库
BitmapFactory.Options bmOptions =新BitmapFactory.Options();
bmOptions.inJustDe codeBounds = TRUE;
BitmapFactory.de codeFILE(mCurrentMediaPath,bmOptions);
位图位图= BitmapFactory.de codeFILE(mCurrentMediaPath,bmOptions); 插入(getBytesFromBitmap(位图));
解决方案
创建图像的位图,则
BMP位图= BitmapFactory.de codeStream(新的FileInputStream(F),NULL,NULL);
ByteArrayOutputStream流=新ByteArrayOutputStream();
bmp.com preSS(Bitmap.Com pressFormat.JPEG,100,流);
字节[]的字节数组= stream.toByteArray();dba.open();dba.insertPhoto(字节阵列);
其中DBA是数据库类的对象。
在数据库类如创建表:
私有静态最后弦乐CREATETABLE_PHOTO =CREATE TABLE eqpphoto(EImage BLOB+);;公共静态最后弦乐TABLE_PHOTO =eqpphoto;众长insertPhoto(字节[] EImage){ 尝试{
的System.out.println(函数调用:);
ContentValues值=新ContentValues(); values.put(EIMAGE,EImage);
返回db.insert(TABLE_PHOTO,空,价值);
}赶上(例外五){
e.printStackTrace();
返回0;
}
}
I'm trying to save a photo as a blob in the SQLite (not just reference it). mCurrentMediaPath is the current path where the photo will be stored. Now, I need to save it to the database after the picture is taken and save button pressed (I guess after the 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;
}
}
Where should I implement the insertion?
//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));
解决方案
Create bitmap of your image then
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);
where dba is object of database class.
create table in database class like:
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系统数据库中的照片作为一个blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文