简体   繁体   English

如何在数据库中保存图像?

[英]how to save images in database?

I have Database, where I have 3 columns (first Id ,second title String , third image int ). 我有数据库,我有3列(第一个Id ,第二个标题String ,第三个image int )。 DB like contact list with photo. 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));

Then with SimpleCursorAdapter I fill the list from names and their photo 然后使用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);

All is work, but when i want to add record to DB and choose a picture from Galery, i don't know how to save it in DB. 一切正常,但是当我想向DB添加记录并从Galery中选择图片时,我不知道如何将其保存在DB中。 I think i can save path to file like String path = "/sdcard/DCIM/Camera/IMG20130222_008.jpg" ; 我认为我可以将文件的String path = "/sdcard/DCIM/Camera/IMG20130222_008.jpg"保存为String path = "/sdcard/DCIM/Camera/IMG20130222_008.jpg" ; But it is proble when i will fill list, becouse in DB image is saving like integer value. 但是当我填写列表时会出现问题,因为在DB映像中保存起来就像整数值一样。 Sorry for my english =( 对不起,我的英语=(

Maybe you should try to save the whole image into db as blob? 也许您应该尝试将整个图像以blob的形式保存到db中?

See Android How to save camera images in database and display another activity in list view? 请参阅Android。如何将相机图像保存在数据库中并在列表视图中显示其他活动?

In this case you have to write your own CursorAdapter, then see this 在这种情况下,您必须编写自己的CursorAdapter,然后再看此
Images in SimpleCursorAdapter SimpleCursorAdapter中的图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM