Android 开发图片压缩/缩略图的方法
FrameLayout fr=(FrameLayout)findViewById(R.id.FrameLayout01);
BitmapFactory.Options options =new BitmapFactory.Options();
options.inJustDecodeBounds =true;
// 获取这个图片的宽和高
Bitmap bitmap =BitmapFactory.decodeFile("/sdcard/test.jpg", options); //此时返回bm为空
options.inJustDecodeBounds =false;
//计算缩放比
int be = (int)(options.outHeight/ (float)200);
if (be <= 0)
be = 1;
options.inSampleSize = be;
//重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false哦
bitmap=BitmapFactory.decodeFile("/sdcard/test.jpg",options);
int w = bitmap.getWidth();
int h = bitmap.getHeight();
System.out.println(w+" "+h);
ImageView iv=new ImageView(this);
iv.setImageBitmap(bitmap);
File file=new File("/sdcard/feng.png");
try {
FileOutputStream out=newFileOutputStream(file);
if(bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)){
out.flush();
out.close();
}
} catch (FileNotFoundException e){
// TODO Auto-generated catchblock
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catchblock
e.printStackTrace();
}
更多相关文章
- android读取系统相机及相册图片(亲测可用)
- react native android 调试时图片没有显示出来
- Android 图片平铺效果bitmap
- Android 保存图片到SQLite
- Android 将图片转化为缩略图再加载
- 一起学android之设置资源图片为圆角图片 (28)
- Android异步加载图片并缓存到内存和SD卡上
- Android中ImageSwitcher结合Gallery展示SD卡中的资源图片
- ImageView 设置图片