Bitmap.createScaledBitmap(src, dstWidth, dstHeight, filter)试试这个方法。不过我之前用会失真。不知为啥。
解决方案 »
- 如何快速播放MP3?
- android游戏开发--三国无双
- struts2 框架异常
- 高手帮忙。code不知道想表面什么意思?
- 有把Android 2.2 Launcher2移植到Android2.1上的成功经验吗?
- Integer... integers中的“...”是什么意思?
- findFragmentById 获取 Fragment为空!!!
- 求助获取手机通讯录联系人的问题????
- android的RSA加密
- 内部类的子线程可以改变外部类的成员变量吗
- Bitmap.createBitmap的options属性inSampleSize的值只能是2的幂吗???
- Android4.2.2 盒子 以太网打不开的问题
不是说Bitmap.createBitmpa的inSampleSize只能是2的幂吗???
这是什么情况???
经过研究,发现,Options中有个属性inJustDecodeBounds,研究了一下,终于明白是什么意思了,SDK中的E文是这么说的
If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.
意思就是说如果该值设为true那么将不返回实际的bitmap不给其分配内存空间而里面只包括一些解码边界信息即图片大小信息,那么相应的方法也就出来了,通过设置inJustDecodeBounds为true,获取到outHeight(图片原始高度)和 outWidth(图片的原始宽度),然后计算一个inSampleSize(缩放值),然后就可以取图片了,这里要注意的是,inSampleSize 可能小于0,必须做判断。
具体代码如下: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);
这样我们就可以读取较大的图片就会避免内存溢出了。如果你想把压缩后的图片保存在Sdcard上的话就很简单了:
File file=new File("/sdcard/feng.png");
try {
FileOutputStream out=new FileOutputStream(file);
if(bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)){
out.flush();
out.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ok,这样就把图片保存在/sdcard/feng.png这个文件里面了
你说的方法确实是最快最节省内存的方法,但是options.inSampleSize据说只能是2的幂,也就是说任意尺寸的图片要压缩成定宽是无法实现的,必然存在剪切。
我是问这个能不能解决???