如何存储(位图图像​​)和检索的SQLite数据库图像中的android?

编程入门 行业动态 更新时间:2024-10-26 04:27:25
本文介绍了如何存储(位图图像​​)和检索的SQLite数据库图像中的android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的项目,我需要将图像存储到SQLite数据库,也需要找回它在我的Andr​​oid模拟器展示。当我直接连接codeD字符串,这是我从Java类中使用套接字,图像显示也得到了解码后显示。但是,通过使用 getblob()功能,它包含了一个当我存储字符串的字节数组code与数据类型BLOB SQLite数据库,然后再检索不同的价值,出现此错误:

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:

JAVA.lang.NULLPointerException: Factory returns null.

我需要一个建议来存储位图图像到一个SQLite数据库,并从SQLite数据库检索。

I need a suggestion to store a bitmap image into a sqlite database and also to retrieve it from the sqlite 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); } }

在数据库中插入:

public void addEntry( String name, byte[] image) throws SQLiteException{ ContentValues cv = new ContentValues(); cv.put(KEY_NAME, name); cv.put(KEY_IMAGE, image); database.insert( DB_TABLE, null, cv ); }

检索数据:

byte[] image = cursor.getBlob(1);

注意:

  • 插入数据库之前,您需要将您的位​​图图像转换为字节数组,然后先用数据库查询应用它。
  • 从数据库中检索时,肯定有图像的字节数组,你需要做的是字节数组转换回原始图像。所以,你必须使用BitmapFactory的脱code。
  • 下面是一个实用工具类,我希望能帮助你:

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

    更多推荐

    如何存储(位图图像​​)和检索的SQLite数据库图像中的android?

    本文发布于:2023-10-07 10:44:24,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1469177.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:图像   位图   数据库   SQLite   android

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!