如何将从 url 检索到的图像存储在 SQLite 数据库中?

发布于 2024-11-26 07:20:12 字数 3017 浏览 0 评论 0原文

我正在从 url 检索图像。 是否可以将图像存储在 SQLite 数据库中,而不是缓存图像?

                /** Simple Constructor saving the 'parent' context. */
                public ImageAdapter(Context c) { this.myContext = c; }





                /** Returns the amount of images we have defined. */
                public int getCount() { return this.myRemoteImages.length; }

                /* Use the array-Positions as unique IDs */
                public Object getItem(int position) { return position; }
                public long getItemId(int position) { return position; }

                /** Returns a new ImageView to
                * be displayed, depending on
                * the position passed. */
                public View getView(int position, View convertView, ViewGroup parent) {
                ImageView i = new ImageView(this.myContext);

                try {

                                URL aURL = new URL(myRemoteImages[position]);
                                URLConnection conn = aURL.openConnection();

                                conn.connect();

                                InputStream is = conn.getInputStream();  
                                /* Buffered is always good for a performance plus. */
                                BufferedInputStream bis = new BufferedInputStream(is);
                                /* Decode url-data to a bitmap. */

                                Bitmap bm = BitmapFactory.decodeStream(bis);
                                bis.close();
                                is.close();
                                Log.v(imageUrl, "Retrieving image");

                                /* Apply the Bitmap to the ImageView that will be returned. */
                                i.setImageBitmap(bm);
                        } catch (IOException e) {

                                Log.e("DEBUGTAG", "Remtoe Image Exception", e);
                        }

                /* Image should be scaled as width/height are set. */
                i.setScaleType(ImageView.ScaleType.FIT_CENTER);
                /* Set the Width/Height of the ImageView. */
                if(Build.VERSION.SDK_INT >= 11){
                    i.setLayoutParams(new Gallery.LayoutParams(450, 300));
                return i;
                }
                else{
                    i.setLayoutParams(new Gallery.LayoutParams(125, 125));
                    return i;
                }

                }

                /** Returns the size (0.0f to 1.0f) of the views
                * depending on the 'offset' to the center. */
                public float getScale(boolean focused, int offset) {
                /* Formula: 1 / (2 ^ offset) */
                return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
                }
                } 

编辑:设置 imageAdapter 以加载图库中的图像

((Gallery) findViewById(R.id.gallery))
                          .setAdapter(new ImageAdapter(MainMenu.this));

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?

                /** Simple Constructor saving the 'parent' context. */
                public ImageAdapter(Context c) { this.myContext = c; }





                /** Returns the amount of images we have defined. */
                public int getCount() { return this.myRemoteImages.length; }

                /* Use the array-Positions as unique IDs */
                public Object getItem(int position) { return position; }
                public long getItemId(int position) { return position; }

                /** Returns a new ImageView to
                * be displayed, depending on
                * the position passed. */
                public View getView(int position, View convertView, ViewGroup parent) {
                ImageView i = new ImageView(this.myContext);

                try {

                                URL aURL = new URL(myRemoteImages[position]);
                                URLConnection conn = aURL.openConnection();

                                conn.connect();

                                InputStream is = conn.getInputStream();  
                                /* Buffered is always good for a performance plus. */
                                BufferedInputStream bis = new BufferedInputStream(is);
                                /* Decode url-data to a bitmap. */

                                Bitmap bm = BitmapFactory.decodeStream(bis);
                                bis.close();
                                is.close();
                                Log.v(imageUrl, "Retrieving image");

                                /* Apply the Bitmap to the ImageView that will be returned. */
                                i.setImageBitmap(bm);
                        } catch (IOException e) {

                                Log.e("DEBUGTAG", "Remtoe Image Exception", e);
                        }

                /* Image should be scaled as width/height are set. */
                i.setScaleType(ImageView.ScaleType.FIT_CENTER);
                /* Set the Width/Height of the ImageView. */
                if(Build.VERSION.SDK_INT >= 11){
                    i.setLayoutParams(new Gallery.LayoutParams(450, 300));
                return i;
                }
                else{
                    i.setLayoutParams(new Gallery.LayoutParams(125, 125));
                    return i;
                }

                }

                /** Returns the size (0.0f to 1.0f) of the views
                * depending on the 'offset' to the center. */
                public float getScale(boolean focused, int offset) {
                /* Formula: 1 / (2 ^ offset) */
                return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
                }
                } 

EDIT: Set the imageAdapter to load images in Gallery

((Gallery) findViewById(R.id.gallery))
                          .setAdapter(new ImageAdapter(MainMenu.this));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撩动你心 2024-12-03 07:20: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;
    }   

这将在运行时将您的 imageUrl 转换为 Drawble,然后将 Drawble 设置为 Gallery 的 Imageview

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

非要怀念 2024-12-03 07:20:12

是的,您可以将图像作为 BLOB 存储在数据库中,

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

以获取

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

    }

//设置 imageView.setImageBitmap(getImageFromBLOB(cursor.getBlob(object.getColumnIndex("book_thumb"))));

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

始终不够 2024-12-03 07:20:12

以下是如何在外部存储中存储数据的链接:外部存储< /a>

该链接解释了放置文件的位置(如果您想使用外部存储),以及如何检查外部存储是否可用。

编辑:“访问外部存储上的文件”主题下解释了如何访问文件。

您应该在 API 8 或更高版本中调用 getExternalFilesDir() 来获取代表您的应用程序根目录的 File。然后,您可以像平常一样读取和写入文件(例如使用 FileWriter 和 FileReader 来读取文本数据)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文