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

前端 未结 3 1608
借酒劲吻你
借酒劲吻你 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:12
    protected Drawable Imagehandler(String url) {
            try {
                url=url.replaceAll(" ", "%20");
                InputStream is = (InputStream)this.fetch(url);
                Drawable d = Drawable.createFromStream(is, "src");
                return d;
            } catch (MalformedURLException e)
            {   
                System.out.println(url);
                System.out.println("error at URI"+e);
                return null;
            } 
            catch (IOException e) 
            {
                System.out.println("io exception: "+e);
                System.out.println("Image NOT FOUND");
                return null;
            } 
        }
    
        protected Object fetch(String address) throws MalformedURLException,IOException {
            URL url = new URL(address);
            Object content = url.getContent();
            return content;
        }   
    

    this will convert your imageUrl to Drawble at runtime, then set the Drawble to Imageview of Gallery

    0 讨论(0)
  • 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"))));

    0 讨论(0)
  • 2020-11-29 11:35

    Here's a link to how you store data in external storage: External storage

    The link explains where to place files (if you want to use external storage), and how to check if external storage is available.

    Edit: How you access files, is explained under the topic "Accessing files on external storage".

    You should in API 8, or above invoke getExternalFilesDir() to get a File that represents your applications root directory. You can then read and write the files as you normally do (using e.g. FileWriter and FileReader for text data)

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