本文介绍了图片来源网址的Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从为此我使用的是网络服务将图像设置:
I am trying to set an image from a web service for which I am using:
private class FetchImageTask extends AsyncTask<String, Integer, Bitmap> {
@Override
protected Bitmap doInBackground(String... arg0) {
Bitmap b = null;
try {
b = BitmapFactory.decodeStream((InputStream) new URL(arg0[0]).getContent());
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return b;
}
}
和试图获取它像
final ImageView imgicon = (ImageView) convertView.findViewById(R.id.imgicon);
new FetchImageTask() {
@Override
protected void onPostExecute(Bitmap result) {
if (result != null) {
imgicon.setImageBitmap(result);
}
}
}.execute("Url/images/"+bitmapname);
不过,这并不显示它也没有任何错误。任何的猜测?
But it doesn't display it nor any error. Any guess?
推荐答案
试试这个:
public Bitmap getBitmapFromURL(String src) {
try {
java.net.URL url = new java.net.URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
有关OutOfMemoryIssue使用:
for OutOfMemoryIssue USe:
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
matrix, false);
return resizedBitmap;
}
这篇关于图片来源网址的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!