Android: Saving Image to Database

不想你离开。 提交于 2019-12-20 07:28:35

问题


I have a activity with 3 ImageViews and a Button. Clicking on the ImageView shows an AlertDialog with a list of images (located in /res/drawable) that the user can select.

I've hit a wall where I don't know how to store the images to the database that were selected in the ImageViews.

Any pointers?


回答1:


I think the preferred solution is to store the images on the SD card and store the file paths in the database




回答2:


An SQLite database supports a NONE type affinity (you can use BLOB in CREATE TABLE statement) which allows you to store binary objects (like an image) inside the database as-is. This is the column type you should assign to the column to store your image in.

Then, when you write your queries, you should parameterize your query, and bind your raw image data into something that can be inserted into the database using whatever API wrapper function supports BLOBs (I assume you have a special one for Android development), and do the same to read the data back. In the SQLite C API, you would use sqlite3_bind_blob(), and sqlite3_result_blob().



来源:https://stackoverflow.com/questions/3791431/android-saving-image-to-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!