【问题标题】:how to save images in database?如何将图像保存在数据库中?
【发布时间】:2013-02-09 03:04:27
【问题描述】:

我有数据库,其中有 3 列(第一个 Id,第二个标题 String,第三个图像 int)。 DB like 联系人列表和照片。

ContentValues cv = new ContentValues();
cv.put(COLUMN_TITLE, "John");
cv.put(COLUMN_IMG_OUTSIDE, R.drawable.john_photo);
db.insert(DB_TABLE, null, cv));

然后用SimpleCursorAdapter 填写姓名和照片的列表

SimpleCursorAdapter scAdapter;
String[] from = new String[] { DataBase.COLUMN_IMG_OUTSIDE,DataBase.COLUMN_TITLE};
int[] to = new int[] { R.id.img, R.id.text1, };

cursor = db.getAll();
startManagingCursor(cursor);

scAdapter = new SimpleCursorAdapter(ListOfItems.this, R.layout.list, cursor, from, to);
listView.setAdapter(scAdapter);

一切正常,但是当我想将记录添加到数据库并从图库中选择一张图片时,我不知道如何将其保存在数据库中。我想我可以像String path = "/sdcard/DCIM/Camera/IMG20130222_008.jpg"这样保存文件的路径;但是当我填写列表时可能会出现问题,因为在 DB 图像中保存的是整数值。对不起我的英语=(

【问题讨论】:

    标签: android database image


    【解决方案1】:

    也许您应该尝试将整个图像作为 blob 保存到数据库中?

    Android How to save camera images in database and display another activity in list view?

    在这种情况下,您必须编写自己的 CursorAdapter,然后查看此
    Images in SimpleCursorAdapter

    【讨论】:

    • 我认为可以在 BD 中添加一列并在那里保存图片路径。然后编写一个自己的适配器,我在其中检查哪一列是图像。如果列整数为空,则生成下一个 italic bold File imgFile = new File(path); Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); myImageView.setImageBitmap(myBitmap);
    • 是的,自定义适配器是要走的路。就我个人而言,我不喜欢使用 blob 来存储图像,但尽可能存储图像路径是我的首选。
    猜你喜欢
    • 2015-01-08
    • 2013-10-04
    • 1970-01-01
    • 2015-12-17
    • 2020-04-07
    • 1970-01-01
    • 2019-12-23
    • 2019-07-26
    • 1970-01-01
    相关资源
    最近更新 更多