本文介绍了保存图像到SQLITE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的数据类型,我应该使用用于存储图像的sqlLite数据库表??

下面是我的尝试:

  @覆盖
公共无效的onCreate(SQLiteDatabase DB){
    // TODO自动生成方法存根
    db.execSQL(CREATE TABLE+ MP_TABLE_NAME +(+
            BaseColumns._ID +INTEGER PRIMARY KEY AUTOINCREMENT,+
            名称文本,+
            土地增值税REAL,+
            液化天然气REAL,+
            );
              );

}
 

解决方案

试试这个..

创建一个表,BLOB列:

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

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

  DefaultHttpClient mHttpClient =新DefaultHttpClient();
HTTPGET mHttpGet =新HTTPGET(图像URL);
HTT presponse mHtt presponse = mHttpClient.execute(mHttpGet);
如果(mHtt presponse.getStatusLine()的getStatus code()== HttpStatus.SC_OK){
    HttpEntity实体= mHtt presponse.getEntity();
    如果(实体!= NULL){
        //插入到数据库
        ContentValues​​值=新ContentValues​​();
        values​​.put(MyBaseColumn.MyTable.ImageField,EntityUtils.toByteArray(实体));
        。getContentResolver()插入(MyBaseColumn.MyTable.CONTENT_URI,价值观);
    }
 }
 

加载BLOB到一个ImageView的:

  ImageView的MYIMAGE =(ImageView的)findViewById(R.id.myImage);
 byte []的BB = cursor.getBlob(cursor.getColumnIndex(MyBaseColumn.MyTable.ImageField));
 myImage.setImageBitmap(BitmapFactory.de codeByteArray(BB,0,bb.length));
 

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

}
解决方案

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

这篇关于保存图像到SQLITE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:53