saving image into SQLITE?

后端 未结 3 1876
醉酒成梦
醉酒成梦 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));
    
    0 讨论(0)
  • 2020-12-09 07:49

    Refer to the SQLite documentation on datatypes.

    You probably want a BLOB type, also known as "binary blob" or "Binary Long OBject". Just be aware that storing really big things in an SQLite database isn't necessarily a good idea - images are usually better off stored in the filesystem if you can manage it.

    0 讨论(0)
  • 2020-12-09 07:51

    Saving Images in to Sqlite is bad idea. You should save its path in to sqlite. Images are big in size comparing to textual information.

    It would be hard for saving and retrieving images from sqlite.

    So, I would like suggest you to store images in external folder and store its path in to sqlite database.

    0 讨论(0)
提交回复
热议问题