ser*_*rgi 5 android inputstream bitmap bufferedinputstream
我正在尝试从网址下载imags然后解码它们.问题是,我不知道它们有多大,如果我立即解码它们,应用程序会因太大的图像而崩溃.
我正在做以下操作,它适用于大多数图像,但是对于其中一些图像,它会抛出java.io.IOException: Mark has been invalidated异常.这不是一个大小问题,因为它发生在75KB或120KB的图像上,而不是20MB或45KB的图像.此格式也不重要,因为它可能发生在jpg或png图像上.
pis是一个InputStream.
Options opts = new BitmapFactory.Options();
BufferedInputStream bis = new BufferedInputStream(pis);
bis.mark(1024 * 1024);
opts.inJustDecodeBounds = true;
Bitmap bmImg=BitmapFactory.decodeStream(bis,null,opts);
Log.e("optwidth",opts.outWidth+"");
try {
bis.reset();
opts.inJustDecodeBounds = false;
int ratio = opts.outWidth/800;
Log.e("ratio",String.valueOf(ratio));
if (opts.outWidth>=800)opts.inSampleSize = ratio;
return BitmapFactory.decodeStream(bis,null,opts);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
Run Code Online (Sandbox Code Playgroud)
小智 6
我想你想要解码大图像.我是通过选择图库图片来做到的.
File photos= new File("imageFilePath that you select");
Bitmap b = decodeFile(photos);
Run Code Online (Sandbox Code Playgroud)
"decodeFile(photos)"功能用于解码大图像.我认为你需要获得图像.png或.jpg formet.
private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=70;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale++;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
Run Code Online (Sandbox Code Playgroud)
您可以使用imageView显示它.
ImageView img = (ImageView)findViewById(R.id.sdcardimage);
img.setImageBitmap(b);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6660 次 |
| 最近记录: |