【问题标题】:Android Drawable Images from URL来自 URL 的 Android 可绘制图像
【发布时间】:2011-03-23 10:55:54
【问题描述】:

我目前正在使用以下代码将图像加载为可绘制对象形成一个 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 版中,当我给它一个 URL 时,它会抛出一个 FileNotFoundException。在 2.2 中,给定完全相同的 URL,它可以正常工作。以下 URL 是我提供此功能的示例输入。

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

如何以与 URL 全面兼容的方式加载图像?

【问题讨论】:

    标签: android url image drawable


    【解决方案1】:

    位图不是可绘制的。如果您真的需要 Drawable,请执行以下操作:

    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中的提示)

    【讨论】:

    • 很好的答案,但请注意构造函数已被弃用。使用 BitmapDrawable(Resources, Bitmap) 确保可绘制对象已正确设置其目标密度
    • 很好,已回答。由于已弃用而应更新
    【解决方案2】:

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

    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 会拒绝访问

    【讨论】:

    • 抛出异常,如 android.os.NetworkOnMainThreadException
    • 这是不正确的,@Prince 使用 AsyncTask 查看我的答案以避免 NetworkOnMainThreadException。
    【解决方案3】:

    我不确定,但我认为 Drawable.createFromStream() 更适用于本地文件,而不是下载的 InputStreams。尝试使用BitmapFactory.decodeStream(),然后将返回位图包装在BitmapDrawable 中。

    【讨论】:

    • 我好像记得以前遇到过这个问题;我当时是 1.5。
    【解决方案4】:

    要从 URL 中获取 Drawable 图像,您必须使用 AsyncTask 以避免 NetWorkOnMainThreadException,并且您可以将在 onPostExecute() 中获得的结果 Drawable 设置为您的 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();
    

    【讨论】:

      【解决方案5】:

      以下代码适用于我:

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

      【讨论】:

        【解决方案6】:

        您可以使用 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 的 BitMap。

        【讨论】:

        • 这个人要求某种方法从一个 url 创建一个可绘制对象,你的一个在下面这样做,没有办法获得 drawable;而是你传递一个视图和库在其上设置从 url 下载的图像。那么如何将其视为问题的答案?
        猜你喜欢
        • 2011-09-12
        • 2011-09-14
        • 1970-01-01
        • 2012-08-23
        • 1970-01-01
        • 1970-01-01
        • 2011-09-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多