代码之家  ›  专栏  ›  技术社区  ›  Señor Reginold Francis

Android可从URL绘制图像

  •  25
  • Señor Reginold Francis  · 技术社区  · 16 年前

    我目前正在使用下面的代码将图像作为可绘制对象加载到URL中。

    Drawable drawable_from_url(String url, String src_name) 
    throws java.net.MalformedURLException, java.io.IOException {
            return Drawable.createFromStream(((java.io.InputStream)new java.net.URL(url).getContent()), src_name);
    
    }
    

    这段代码完全可以正常工作,但似乎存在兼容性问题。在版本1.5中,它抛出 FileNotFoundException

    http://bks6.books.google.com/books?id=aH7BPTrwNXUC&printsec=frontcover&img=1&zoom=5&edge=curl&sig=ACfU3U2aQRnAX2o2ny2xFC1GmVn22almpg
    

    5 回复  |  直到 11 年前
        1
  •  49
  •   Joonsoo Cris    7 年前

    public static Drawable drawableFromUrl(String url) throws IOException {
        Bitmap x;
    
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.connect();
        InputStream input = connection.getInputStream();
    
        x = BitmapFactory.decodeStream(input);
        return new BitmapDrawable(Resources.getSystem(), x);
    }
    

    (我使用了在 https://stackoverflow.com/a/2416360/450148 )

        2
  •  15
  •   OneCricketeer Gabriele Mariotti    8 年前

    我自己解决的。我使用下面的代码将其作为位图加载。

    Bitmap drawable_from_url(String url) throws java.net.MalformedURLException, java.io.IOException {
    
        HttpURLConnection connection = (HttpURLConnection)new URL(url) .openConnection();
        connection.setRequestProperty("User-agent","Mozilla/4.0");
    
        connection.connect();
        InputStream input = connection.getInputStream();
    
        return BitmapFactory.decodeStream(input);
    }
    

    另外,添加用户代理也很重要,因为googlebooks拒绝用户代理的访问

        3
  •  5
  •   Dan Lew    16 年前

    我不确定,但我认为Drawable.createFromStream()更适合用于本地文件,而不是下载的InputStreams。尝试使用 BitmapFactory.decodeStream() BitmapDrawable .

        4
  •  1
  •   Aziz Shaikh    13 年前

    Matrix Mat = new Matrix();
    
    Bitmap Source = BitmapFactory.decodeFile("ItemImagePath");
    
    Bitmap Destination = Bitmap.createScaledBitmap( Source, 320, 320, true );
    
    Source = Bitmap.createBitmap( Destination, 0, 0, Destination.getWidth(), Destination.getHeight(),Mat, true );
    
    ItemImageView.setImageBitmap(Source);
    
        5
  •  1
  •   Adriaan Koster    12 年前

    您可以使用com.androidquery.androidquery非常简单地完成这项工作。例如:

    AQuery aq = new AQuery(this);
    aq.id(view).image("http://yourserver/yourimage.png", true, true, 300, new BitmapAjaxCallback() {
            @Override
            public void callback(String url, ImageView imageView, Bitmap bitmap, AjaxStatus status) {
                Drawable drawable = new BitmapDrawable(getResources(), bm);
            }
        });
    

    如果使用BitmapAjaxCallback,则可以访问位图,该位图可以包装为BitmapDrawable。

        6
  •  1
  •   Jorgesys    7 年前

    AsyncTask 避免 NetWorkOnMainThreadException ,并在 onPostExecute() 您可以设置为ImageView:

        final String urlImage = "https://www.android.com/static/2016/img/hero-carousel/banner-android-p-2.jpg";
    
        new AsyncTask<String, Integer, Drawable>(){
    
            @Override
            protected Drawable doInBackground(String... strings) {
                Bitmap bmp = null;
                try {
                    HttpURLConnection connection = (HttpURLConnection) new URL(urlImage).openConnection();
                    connection.connect();
                    InputStream input = connection.getInputStream();
                    bmp = BitmapFactory.decodeStream(input);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return new BitmapDrawable(bmp);
            }
    
            protected void onPostExecute(Drawable result) {
    
                //Add image to ImageView
                myImageView.setImageDrawable(result);
    
            }
    
    
        }.execute();