How to store image retrieved from url in a SQLite database?

前端 未结 3 1627
借酒劲吻你
借酒劲吻你 2020-11-29 10:38

I am retrieving images from a url. Instead of caching the images, would it by any chance be possible to store it in a SQLite database?

                /**          


        
3条回答
  •  广开言路
    2020-11-29 11:35

    ya you can store image as BLOB in your database,

    public static byte[] urlToImageBLOB(String url) throws IOException {
            httpclient = new DefaultHttpClient();
            entity = null;
            httpGet = new HttpGet(url);
            response = httpclient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                entity = response.getEntity();
            }
            return EntityUtils.toByteArray(entity);
        } 
    

    To fetch

    public static Bitmap getImageFromBLOB(byte[] mBlob) {
            byte[] bb = mBlob;
            return BitmapFactory.decodeByteArray(bb, 0, bb.length);
    
        }
    

    // to set imageView.setImageBitmap(getImageFromBLOB(cursor.getBlob(object.getColumnIndex("book_thumb"))));

提交回复
热议问题