Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

android - saving image into SQLITE?

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

the following is my attempt:

@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,"+
            ");"
              );

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...