来自 URL 的 Android 可绘制图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3375166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Drawable Images from URL
提问by Se?or Reginold Francis
I am presently using the following piece of code to load in images as drawable objects form a URL.
我目前正在使用以下代码将图像加载为可绘制对象形成 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);
}
This code works exactly as wanted, but there appears to be compatibility problems with it. In version 1.5, it throws a FileNotFoundException
when I give it a URL. In 2.2, given the exact same URL, it works fine. The following URL is a sample input I am giving this function.
此代码完全按预期工作,但似乎存在兼容性问题。在 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
How would I load in images in a way that is compatible across the board from a URL?
我将如何以与 URL 全面兼容的方式加载图像?
采纳答案by Se?or Reginold Francis
Solved it myself. I loaded it in as a bitmap using the following code.
自己解决了。我使用以下代码将其作为位图加载。
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);
}
It was also important to add in the user agent, as googlebooks denies access if it is absent
添加用户代理也很重要,因为如果不存在,googlebooks 会拒绝访问
回答by Felipe
Bitmap is not a Drawable. If you really need a Drawable do this:
位图不是可绘制的。如果您真的需要 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);
}
(I used the tip found in https://stackoverflow.com/a/2416360/450148)
回答by Dan Lew
I'm not sure, but I think that Drawable.createFromStream() is more intended for use with local files rather than downloaded InputStreams. Try using BitmapFactory.decodeStream()
, then wrapping the return Bitmap in a BitmapDrawable.
我不确定,但我认为 Drawable.createFromStream() 更适合用于本地文件而不是下载的 InputStreams。尝试使用BitmapFactory.decodeStream()
,然后将返回的 Bitmap 包装在BitmapDrawable 中。
回答by Dhukkaieswaran.N
The following code works for me:
以下代码对我有用:
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);
回答by Adriaan Koster
You can use com.androidquery.AndroidQuery to do this quite simply. For example:
您可以使用 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);
}
});
If you use the BitmapAjaxCallback you will get access to the BitMap which you can wrap as a BitmapDrawable.
如果您使用 BitmapAjaxCallback,您将可以访问可以包装为 BitmapDrawable 的 BitMap。
回答by Jorgesys
To get a Drawable image from an URL you must use an AsyncTask
to avoid NetWorkOnMainThreadException
, and the result Drawable obtained in onPostExecute()
you can set to your ImageView:
要从 URL 获取 Drawable 图像,您必须使用AsyncTask
来避免NetWorkOnMainThreadException
,并且onPostExecute()
您可以将在您的 ImageView 中获得的结果 Drawable设置为:
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();