2013-02-23 143 views
0

我有数据库,我有3列(第一Id,第二标题String,第三图像int)。 DB喜欢带照片的联系人列表。如何将图像保存到数据库中?

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); 

全部是工作,但是当我想添加记录数据库,然后从Galery一幅画,我不知道如何将其保存在数据库中。我想我可以保存文件的路径,如String path = "/sdcard/DCIM/Camera/IMG20130222_008.jpg";但是,当我填充列表时,会出现问题,因为数据库图像正像整数值一样保存。对不起,我的英语=(

回答

1

也许你应该尝试将整个图像保存到数据库的BLOB?

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

在这种情况下,你必须编写自己的CursorAdapter,然后看到这个
Images in SimpleCursorAdapter

+0

我认为ican在BD中添加一列,然后保存一个图片路径,然后编写一个自己的适配器,用于检查哪一列是一个图像,如果列整数为空,则下一个 _italic_ ** bold ** '文件imgFile =新文件(路径); 位图myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); myImageView.setImageBitmap(myBitmap);' – 2013-02-23 15:41:40

+0

是的,自定义适配器是要走的路。就我个人而言,我不喜欢用blob来存储图像,但只要有可能,存储图像路径是我的第一选择。 – 2013-02-24 03:11:10