我想在后台线程中从Url加载Bitmap。我的方法如下所示。
public Bitmap decodeSampledBitmapFromURL(String url,int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
InputStream is=null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Rect rect = new Rect(-1, -1, -1, -1);
Bitmap bmp1 = BitmapFactory.decodeStream(is,rect, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bmp2 = BitmapFactory.decodeStream(is,rect, options);
return bmp2;
}
我总是将bmp1和bmp2视为null。为什么呢?
答案 0 :(得分:0)
可能检查输入流是否为空,以查看是否有错误的位图代码或获取输入流代码。 也许Rect应该有适当的尺寸?
答案 1 :(得分:0)
您正在使用解码边界的选项对流进行解码,因此显然位置输出将为空,然后您尝试解码来自相同流的位图,该位图已在第一次解码中使用。您可以尝试将流读取到字节数组缓冲区然后包装bytearrayInputstreams,记得为每次解码创建新流