我想从输入流中获取位图,然后重新调整大小。但我收到了以下错误。
如果我没有重新调整大小就返回,它可以正常工作。
有人可以帮忙吗?
logcat的:
01-07 01:38:33.412: D/skia(1307): --- SkImageDecoder::Factory returned null
CODE:
private Bitmap getBitmap(String url)
{
try {
Bitmap bitmap=null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.setDoInput(true);
conn.setInstanceFollowRedirects(true);
conn.connect();
InputStream is=conn.getInputStream();
//return BitmapFactory.decodeStream(is); // THIS WORKS FINE
bitmap = decodeFile(is);
is.close();
return bitmap;
} catch (Exception ex){
return null;
}
}
private Bitmap decodeFile(InputStream istream){
BufferedInputStream is = new BufferedInputStream(istream);
try {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is,null,o);
final int REQUIRED_SIZE=60;
int height_tmp=o.outHeight;
int scale=1;
while(true){
if(height_tmp/2<REQUIRED_SIZE)
break;
height_tmp/=2;
scale*=2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
Log.d("insample", "aa: "+scale);
return BitmapFactory.decodeStream(is, null, o2);
} catch (Exception e) {
Log.d("aaa","aa: "+e);
} finally{
try {
is.close();
} catch( IOException ignored ) {}
}
return null;
}
答案 0 :(得分:-3)
at
iv.setImageResource(resId);
resId是无效值...
将其更改为
iv.setImageResource(R.drawable.ic_launcher);
并测试代码