关于Bitmap压缩的问题
本人刚入门新手一枚。今天在练习bitmap内存缓存和本地缓存。
使用到Bitmapfactory.Options 的时候遇到了一点问题,
在Options设置之后无法获取到bitmap。
代码如下:
private Bitmap downloadBitmap(String imageUrl) {
HttpURLConnection conn = null;
try {
URL url = new URL(imageUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
conn.setReadTimeout(5 * 1000);
if (conn.getResponseCode() == 200) {
//Bitmap bitmap = BitmapFactory.decodeStream(conn.getInputStream());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(conn.getInputStream(), null, options);
options.inSampleSize = calculateInSampleSize(options,
PixelUtils.dip2px(context, 200), PixelUtils.dip2px(context, 250));
options.inPreferredConfig = Config.RGB_565;
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeStream(conn.getInputStream(), null,options);
if (bitmap != null) {
return bitmap;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}
return null;
}
}
而不用options直接加载就可以成功获取.上断代码注释的那一行。
在网上也看了一些文章,没发现什么问题,但Bitmap始终返回null(options.inJustDecodeBounds = false之后),
研究了一下午还是没解决,特来请教
求大神不吝赐教,感激不尽。