[英]Android load image from url and show imageview
i try to load image fro url and show imageview.i wrote code witch can download image and show it.but now i want to download facebook user image and show it.i know how i can check facebook user image "like this http://graph.facebook.com/user id /picture?width=80&height=80 when i run programm i have RuntimeException this is a my source 我尝试从URL加载图像并显示imageview.i写代码,女巫可以下载图像并显示它。但是现在我想下载facebook用户图像并显示它。我知道如何检查“ facebook用户图像”,例如http:// /graph.facebook.com/user id / picture?width = 80&height = 80当我运行程序时,我有RuntimeException这是我的来源
private class GetXMLTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... urls) {
Bitmap map = null;
for (String url : urls) {
map = downloadImage(url);
}
return map;
}
// Sets the Bitmap returned by doInBackground
@Override
protected void onPostExecute(Bitmap result) {
user_img.setImageBitmap(result);
}
// Creates Bitmap from InputStream and returns it
private Bitmap downloadImage(String url) {
Bitmap bitmap = null;
InputStream stream = null;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
try {
stream = getHttpConnection(url);
bitmap = BitmapFactory.decodeStream(stream, null, bmOptions);
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}
// Makes HttpURLConnection and returns InputStream
private InputStream getHttpConnection(String urlString)
throws IOException {
InputStream stream = null;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
try {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
stream = httpConnection.getInputStream();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return stream;
}
}
new GetXMLTask().execute((new String[] { URL }));
if my url would be for example http://mywebsite./pic.jpg then program working perfect but i want show facebook user image 如果我的网址是例如http://mywebsite./pic.jpg,则程序运行正常,但我想显示Facebook用户图像
You should be using picasso. 您应该使用毕加索。 Add dependency into your project.
将依赖项添加到您的项目中。 And further just a singe line of code will do the task.
而且,只有一小段代码才能完成任务。 Picasso.with(context).load(" http://somepath/someimage.png ").into(imageView);
Picasso.with(context).load(“ http://somepath/someimage.png ”).into(imageView); No need of writing thread and optimizing bitmaps.
无需编写线程和优化位图。
You can refer : http://square.github.io/picasso/ 您可以参考: http : //square.github.io/picasso/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.