将图像保存到SQLITE中?

0 sqlite android

我应该使用哪种数据类型在sqlLite数据库表中存储图像?

以下是我的尝试:

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL("CREATE TABLE" + MP_TABLE_NAME + " ( " +
            BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            " NAME TEXT, " +
            " LAT REAL, "+
            " LNG REAL,"+
            ");"
              );

}
Run Code Online (Sandbox Code Playgroud)

小智 10

试试这个..

使用BLOB列创建表:

CREATE TABLE storedImages (_id INTEGER PRIMARY KEY, myImage BLOB);
Run Code Online (Sandbox Code Playgroud)

使用HTTP下载图像并将其存储在表中:

DefaultHttpClient mHttpClient = new DefaultHttpClient();  
HttpGet mHttpGet = new HttpGet("your image url");  
HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);  
if (mHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
    HttpEntity entity = mHttpResponse.getEntity();  
    if (entity != null) {  
        // insert to database  
        ContentValues values = new ContentValues();  
        values.put(MyBaseColumn.MyTable.ImageField, EntityUtils.toByteArray(entity));  
        getContentResolver().insert(MyBaseColumn.MyTable.CONTENT_URI, values);  
    }
 }
Run Code Online (Sandbox Code Playgroud)

将BLOB加载到ImageView中:

 ImageView myImage = (ImageView) findViewById(R.id.myImage);
 byte[] bb = cursor.getBlob(cursor.getColumnIndex(MyBaseColumn.MyTable.ImageField));
 myImage.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0, bb.length));
Run Code Online (Sandbox Code Playgroud)


Li-*_*Yip 8

请参阅有关数据类型SQLite文档.

您可能需要一种BLOB类型,也称为"二进制blob"或"二进制长对象".请注意,在SQLite数据库中存储大量内容并不一定是个好主意 - 如果您可以管理图像,通常最好将图像存储在文件系统中.


小智 5

将图像保存到Sqlite是个坏主意.您应该将其路径保存到sqlite.与文本信息相比,图像的大小很大.

从sqlite保存和检索图像会很困难.

因此,我建议您将图像存储在外部文件夹中,并将其路径存储到sqlite数据库中.