我尝试加载图片从网址和显示imageview.i写代码女巫可以下载图像并显示它。但现在我想下载Facebook用户图像并显示它。我知道如何检查Facebook用户图像“喜欢这个 http://graph.facebook.com/user id / picture?width = 80& height = 80 当我运行programm时,我有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 }));
如果我的网址是例如http://mywebsite./pic.jpg,那么程序工作正常但我想要显示facebook用户图像
答案 0 :(得分:0)
你应该使用毕加索。 在项目中添加依赖项。而且只需要一行代码即可完成任务。 Picasso.with(上下文).load(&#34; http://somepath/someimage.png&#34)。到(ImageView的); 无需编写线程和优化位图。