如何在android中从sqlite数据库存储(位图图像)和检索图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11790104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to store(bitmap image) and retrieve image from sqlite database in android?
提问by Rahul Gautam
In my project I need to store an image into a sqlite database and also need to retrieve it to show in my android emulator. When I show it directly after decoding the encoded string, which I got from Java class using sockets, the image displays there. But when I store a byte array code of the string into the sqlite database with the datatype blob and then again retrieve it by using the getblob()
function it contains a different value and this error occurs:
在我的项目中,我需要将图像存储到 sqlite 数据库中,还需要检索它以在我的 android 模拟器中显示。当我在解码使用套接字从 Java 类获得的编码字符串后直接显示它时,图像显示在那里。但是,当我使用数据类型 blob 将字符串的字节数组代码存储到 sqlite 数据库中,然后使用getblob()
包含不同值的函数再次检索它时,会发生此错误:
JAVA.lang.NULLPointerException: Factory returns null.
I need a suggestion to store a bitmap image into a sqlite database and also to retrieve it from the sqlite database.
我需要建议将位图图像存储到 sqlite 数据库中,并从 sqlite 数据库中检索它。
回答by Lazy Ninja
Setting Up the database
设置数据库
public class DatabaseHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "database_name";
// Table Names
private static final String DB_TABLE = "table_image";
// column names
private static final String KEY_NAME = "image_name";
private static final String KEY_IMAGE = "image_data";
// Table create statement
private static final String CREATE_TABLE_IMAGE = "CREATE TABLE " + DB_TABLE + "("+
KEY_NAME + " TEXT," +
KEY_IMAGE + " BLOB);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// creating table
db.execSQL(CREATE_TABLE_IMAGE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// on upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
// create new table
onCreate(db);
}
}
Insert in the Database:
插入数据库:
public void addEntry( String name, byte[] image) throws SQLiteException{
SQLiteDatabase database = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_IMAGE, image);
database.insert( DB_TABLE, null, cv );
}
Retrieving data:
检索数据:
byte[] image = cursor.getBlob(1);
Note:
笔记:
- Before inserting into database, you need to convert your Bitmap image into byte array first then apply it using database query.
- When retrieving from database, you certainly have a byte array of image, what you need to do is to convert byte array back to original image. So, you have to make use of BitmapFactory to decode.
- 在插入数据库之前,您需要先将位图图像转换为字节数组,然后使用数据库查询应用它。
- 从数据库中检索时,您肯定有一个图像字节数组,您需要做的是将字节数组转换回原始图像。因此,您必须使用 BitmapFactory 进行解码。
Below is an Utility class which I hope could help you:
下面是一个实用程序类,我希望可以帮助您:
public class DbBitmapUtility {
// convert from bitmap to byte array
public static byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, stream);
return stream.toByteArray();
}
// convert from byte array to bitmap
public static Bitmap getImage(byte[] image) {
return BitmapFactory.decodeByteArray(image, 0, image.length);
}
}
Further reading
If you are not familiar how to insert and retrieve into a database, go through this tutorial.
进一步阅读
如果您不熟悉如何在数据库中插入和检索,请阅读本教程。
回答by Yash
If you are working with Android's MediaStore database, here is how to store an image and then display it after it is saved.
如果您正在使用 Android 的 MediaStore 数据库,这里是如何存储图像,然后在保存后显示它。
on button click write this
在按钮上点击写这个
Intent in = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
in.putExtra("crop", "true");
in.putExtra("outputX", 100);
in.putExtra("outputY", 100);
in.putExtra("scale", true);
in.putExtra("return-data", true);
startActivityForResult(in, 1);
then do this in your activity
然后在您的活动中执行此操作
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
Bitmap bmp = (Bitmap) data.getExtras().get("data");
img.setImageBitmap(bmp);
btnadd.requestFocus();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String encodedImageString = Base64.encodeToString(b, Base64.DEFAULT);
byte[] bytarray = Base64.decode(encodedImageString, Base64.DEFAULT);
Bitmap bmimage = BitmapFactory.decodeByteArray(bytarray, 0,
bytarray.length);
}
}