31

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

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

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

4

6 回答 6

50

位图不是可绘制的。如果您真的需要 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中的提示)

于 2012-02-28T21:31:34.923 回答
15

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

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

于 2010-08-03T14:44:31.937 回答
5

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

于 2010-07-30T20:47:44.037 回答
2

要从 URL 中获取 Drawable 图像,您必须使用AsyncTaskto NetWorkOnMainThreadExceptionAvoid ,并且您可以将从中获得的结果 DrawableonPostExecute()设置为您的 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();
于 2019-05-07T18:06:13.193 回答
1

以下代码适用于我:

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);
于 2012-10-30T10:04:13.533 回答
0

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

于 2013-07-15T14:21:20.380 回答