【问题标题】:Store & retrieve image into SQLite Database将图像存储和检索到 SQLite 数据库
【发布时间】:2015-03-16 18:44:48
【问题描述】:

我要将可绘制文件夹中的图像存储到 SQlite 数据库中,然后简单地检索它。我稍后会修改这个概念,但我在 ImageView 中什么都没有。

代码如下:

public class DBHelper extends SQLiteOpenHelper {

    protected static final String DATABASE_NAME = "kamuskerul2";
    private static final int DB_VERSION = 1;
    private final Context myContext;

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DB_VERSION);
        this.myContext = context;
    }

    public void onCreate(SQLiteDatabase sqlitedatabase) {

        sqlitedatabase.execSQL("CREATE TABLE tableimage (image BLOB);");
        Bitmap mybitmap = BitmapFactory.decodeResource(myContext.getResources(), R.drawable.me);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        mybitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
        byte[] img = bos.toByteArray();

        ContentValues myContentValues = new ContentValues();
        myContentValues.put("imag", img);

        sqlitedatabase.execSQL("INSERT INTO tableimage (image) VALUES('"+myContentValues+"');");


    }// END of onCreate().


    public void onUpgrade(SQLiteDatabase sqlitedatabase, int i, int j) { 
        if (i >= j) {
            return;
        } else {
            sqlitedatabase.execSQL("DROP DATABASE IF EXISTS kamuskerul2;");
            onCreate(sqlitedatabase);
            return;
        }
     }
  }

还有另一个类(检索图像并显示它)。

private DBHelper dbhelper = new DBHelper(this);
private SQLiteDatabase db;  
ImageView myTestedImageView;
byte[] imgimg;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.MainActivity);

    db = dbhelper.getReadableDatabase();

    // ImageView
    myTestedImageView = (ImageView)findViewById(R.id.myTestedImageViewXML);

}// end onCreate



public void onClick(View v) {
    String image_name = "myContentValues";
    Cursor cursor_image = db.rawQuery("SELECT * FROM tableimage " + "WHERE image='" + image_name + "';", null);

    if (cursor_image.getCount() != 0) {

            cursor_image.moveToFirst();
            do {
                imgimg =   cursor_image.getBlob(cursor_image.getColumnIndex("image"));
            } while (cursor_image.moveToNext());

            Bitmap b1 = BitmapFactory.decodeByteArray(imgimg, 0, imgimg.length);
            myTestedImageView.setImageBitmap(b1);

}

当我运行应用程序时,什么都没有显示并且数据库是空的。 希望你能理解我的想法并帮助我克服这个问题。

【问题讨论】:

    标签: android database image sqlite blob


    【解决方案1】:

    与其将图像的字节保存在数据库中,不如将图像克隆到应用程序的外部存储中。您可以在数据库中保存路径或只保存图像的名称。

    关于文件存储选项的 Android 文档:http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

    在我看来,将 SQLite 数据库中的图像保存为 blob 是一种不好的做法,即使您将图像转换为字节也是如此。

    【讨论】:

    • 所以你知道任何一步一步实现我的目标的教程。不知何故我知道 android 并且需要更多建议/搜索。 TNX。
    • 我的答案中的链接有点像关于如何在 android 设备上存储图像的教程。只需获取该文件的路径并将其作为字符串存储在 sqlite 数据库中即可。
    猜你喜欢
    • 1970-01-01
    • 2015-08-01
    • 2012-06-04
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多