saving image into SQLITE?

后端 未结 3 1887
醉酒成梦
醉酒成梦 2020-12-09 07:32

which data type should I use for storing an image in sqlLite database table??

the following is my attempt:

@Override
public void onCreate(SQLiteDatab         


        
3条回答
  •  难免孤独
    2020-12-09 07:42

    Try this..

    Creating a table with a BLOB column:

    CREATE TABLE storedImages (_id INTEGER PRIMARY KEY, myImage BLOB);
    

    Downloading an image using HTTP and storing it in your table:

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

    Loading the BLOB into an 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));
    

提交回复
热议问题