Bitmap的使用 防止OOM异常
来源:互联网 发布:新手护肤步骤知乎 编辑:程序博客网 时间:2024/05/01 13:27
public class MainActivity extends Activity {ImageView captruepic;int requestCode = 0;String path = "/mnt/sdcard/pic.jpg";int height = 0;int width = 0;File file = new File(path);@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);captruepic = (ImageView) findViewById(R.id.captruepic);Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));startActivityForResult(intent, requestCode);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {try {if (requestCode == this.requestCode && resultCode == RESULT_OK) {// Bundle bundle = data.getExtras();// (Bitmap)bundle.get("data");Bitmap bit = null;BitmapFactory.Options options = new BitmapFactory.Options();// options.inJustDecodeBounds = true; 表示只返回照片的尺寸值 不解码照片本身options.inJustDecodeBounds = true;bit = BitmapFactory.decodeFile(file.getAbsolutePath(), options);// 这时候options.outHeight options.outWidth 记录的就是照片的尺寸height = options.outHeight;width = options.outWidth;Display display = this.getWindowManager().getDefaultDisplay();if (options.outHeight > 0 && options.outWidth > 0) {// 一般手机拍照的照片的尺寸都比手机屏幕的尺寸大 所以这边用照片的高 宽 除以 屏幕的高 宽int Height = options.outHeight / display.getHeight();int Width = options.outWidth / display.getWidth();// inSampleSize If set to a value > 1, requests the decoder// to subsample the original image, returning a smaller// image to save memory 简单的说就是inSampleSize > 1// 时候返回的照片是原照片的1/inSampleSize倍options.inSampleSize = Height > Width ? Height : Width;}// options.inJustDecodeBounds = false; 表示解码照片本身options.inJustDecodeBounds = false;bit = BitmapFactory.decodeFile(file.getAbsolutePath(), options);captruepic.setImageBitmap(bit);}} catch (Exception e) {e.printStackTrace();}}}
使用Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 开启Camera
如果直接执行startActivityForResult(intent, requestCode);那么在 onActivityResult(int requestCode, int resultCode, Intent data)中的data参数会返回拍的照片
使用Bundle bundle = data.getExtras();
Bitmap bit = (Bitmap)bundle.get("data");获得
如果使用Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 开启Camera
然后设置了intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));照片保存的文件的Uri 那么在 onActivityResult(int requestCode, int resultCode, Intent data)中的data参数不会返回拍的照片,要想获得照片可以通过 BitmapFactory.decodeFile(file.getAbsolutePath(), options);
补充说明:
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
这句话中的MediaStore.EXTRA_OUTPUT是为了告诉android设备不要将返回的图片压缩成很小的尺寸,而且它将告诉应用程序,你想将图片保存在哪个位置。
- Bitmap的使用 防止OOM异常
- 关于大图片加载、转换角度或者生成新的BitMap的时候,防止OOM异常。
- 处理Bitmap出现oom的异常
- 应用中使用Bitmap的时候如何防止OOM的产生
- 应用中使用Bitmap的时候如何防止OOM的产生
- Drawable装Bitmap,防止OOM
- Bitmap加载防止出现OOM
- Android 之 Bitmap 防止OOM
- 安卓 bitmap的对应处理 防止oom
- android使用bitmap的OOM心得
- Android 图片压缩展示 防止使用MediaStore.Images.Media.getBitmap造成的OOM异常
- bitmap造成OOM异常的解决方法-(重要文档)
- Android Bitmap 防止OOM及其他操作
- Bitmap的OOM问题解决
- bitmap的oom问题
- 使用Bitmap将自身保存为文件,BitmapFactory从File中解析图片并防止OOM
- android中关于bitmap造成的OOM内存溢出防止方法
- bitmap造成OOM异常解决例子
- 国际化设置
- 很全的FreeMarker教程
- 根据姓名获取拼音 C#
- 并行程序设计的常见问题
- xampp重置密码
- Bitmap的使用 防止OOM异常
- Hibernate中常见的异常总结
- 国际化未知参数值设置
- android中的左右滑屏实现By ViewPager
- Android APP在Google play中搜索不到或显示不兼容
- linux touch命令
- Zend Framework国际化 一
- Ubuntu下自动启动终端并运行脚本或命令
- 利用virt-manager安装虚拟机