如何存储图像的URL在SQLite数据库retreived?图像、数据库、SQLite、URL

2023-09-11 20:37:01 作者:肥崽

我从一个在检索的URL的图像。 相反,缓存图像,将其通过任何机会可以将其存储在一个SQLite数据库?

  / **简单的构造保存'父'上下文。 * /
                公共ImageAdapter(上下文C){this.myContext = C; }





                / **返回我们已经定义的图像数量。 * /
                公众诠释getCount将(){返​​回this.myRemoteImages.length; }

                / *使用数组位为唯一ID * /
                公共对象的getItem(INT位置){返回位置; }
                众长getItemId(INT位置){返回位置; }

                / **返回一个新的ImageView来
                *被显示,这取决于
                *位置通过。 * /
                公共查看getView(INT位置,查看convertView,ViewGroup中父){
                ImageView的我=新ImageView的(this.myContext);

                尝试 {

                                URL aURL =新的URL(myRemoteImages [位置]);
                                URLConnection的康恩= aURL.openConnection();

                                conn.connect();

                                InputStream的是= conn.getInputStream();
                                / *缓冲总是好的一个表现加分。 * /
                                的BufferedInputStream双=新的BufferedInputStream(是);
                                / *德code URL数据为位图。 * /

                                位图BM = BitmapFactory.de codeStream(之二);
                                bis.close();
                                is.close();
                                Log.v(IMAGEURL,检索图像);

                                / *将位图到将要返回的ImageView的。 * /
                                i.setImageBitmap(BM);
                        }赶上(IOException异常E){

                                Log.e(DEBUGTAG,Remtoe图像异常,E);
                        }

                / *图片应缩放为宽/高设置。 * /
                i.setScaleType(ImageView.ScaleType.FIT_CENTER);
                / *设置的ImageView的宽度/高度。 * /
                如果(Build.VERSION.SDK_INT> = 11){
                    i.setLayoutParams(新Gallery.LayoutParams(450,300));
                返回我;
                }
                其他{
                    i.setLayoutParams(新Gallery.LayoutParams(125,125));
                    返回我;
                }

                }

                / **返回的视图的大小(0.0至1.0F)
                *根据偏移的中心。 * /
                公众持股量中的getScale(布尔专注,诚信偏移){
                / *式:1 /(2 ^偏移)* /
                返回Math.max(0,1.0F /(浮点)Math.pow(2,Math.abs(偏移)));
                }
                }
 

编辑:设置imageAdapter加载在多媒体图像

 ((图库论坛)findViewById(R.id.gallery))
                          .setAdapter(新ImageAdapter(MainMenu.this));
 

解决方案

 保护绘制对象Imagehandler(字符串URL){
        尝试 {
            网址= url.replaceAll(,20%);
            InputStream的是=(的InputStream)this.fetch(URL);
            绘制对象D = Drawable.createFromStream(是,SRC);
            返回D组;
        }赶上(MalformedURLException的E)
        {
            的System.out.println(URL);
            的System.out.println(错误的URI+ E);
            返回null;
        }
        赶上(IOException异常E)
        {
            的System.out.println(IO异常:+ E);
            的System.out.println(没有找到图片);
            返回null;
        }
    }

    保护对象获取(字符串地址)抛出MalformedURLException的,IOException异常{
        网址URL =新的URL(地址);
        对象内容= url.getContent();
        返回的内容;
    }
 
如何创建sqlite数据库

这将您的IMAGEURL转换为Drawble在运行时,然后设置Drawble到图库中的ImageView

I am retreiving 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));

解决方案

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

 
精彩推荐
图片推荐