android 手机拍照后获取图片导致OOM问题

1.拍摄图片后获取的图片大小为 11M  

通过一下方法获取到图片

Uri takePhotoUri = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(),
                            f.getAbsolutePath(), null, null));
但是有时候会导致OOM

2.所以改为在拍照的时候设置图片存储路径

private String mTakePhotoName;
    private void takePhoto() {
        String status = Environment.getExternalStorageState();
        if (status.equals(Environment.MEDIA_MOUNTED)) {
            try {
                mTakePhotoName = System.currentTimeMillis() + ".png";
                File dir = new File(Environment.getExternalStorageDirectory() + "/FollowMePhoto");
                if (!dir.exists()) dir.mkdirs();
                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                File f = new File(dir, mTakePhotoName);
                Uri u = Uri.fromFile(f);
                intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
//                intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, 3 * 1024 );
                startActivityForResult(intent, Constants.ActivityReturnReuqestCode.TAKE_PHOTO);
            } catch (ActivityNotFoundException e) {
//                Toast.makeText(PersonalInfoActivity.this, "没有找到储存目录", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        } else {
        }
    }

然后在返回中处理

String path = Environment.getExternalStorageDirectory()
                    + "/FollowMePhoto"+"/" + mTakePhotoName;
            File f = new File(path);
            try {
                //检测图片是否旋转了,如果旋转,则旋转回来
                int degree = PictureUtil.getBitmapDegree(path);
                if(degree != 0) {
                    Bitmap bitmap = PictureUtil.rotateBitmapByDegree(BitmapUtil.getBitmap(path), degree);
                    Uri takePhotoUri = UriUtil.getUriFromBitmap(this, bitmap);
                    if(null != takePhotoUri)
                        cropImageUri(takePhotoUri, Constants.ActivityReturnReuqestCode.HEAD_PORTRAIT_CROP);
                } else {
//
                    long pictureSize = f.getTotalSpace() / 1024 / 1024 / 1024;
                    LogUtils.i("Picture size = " + pictureSize);
                    LogUtils.i("Picture url = " + path);
                    Uri takePhotoUri = Uri.parse("file:" + path);

//                    Uri takePhotoUri = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(),
//                            f.getAbsolutePath(), null, null));
                    cropImageUri(takePhotoUri, Constants.ActivityReturnReuqestCode.HEAD_PORTRAIT_CROP);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
这样就不会有第一种方式去处理Bitmap而产生的OOM呢,从选择图片到切图界面速度也会快一点。


你可能感兴趣的:(android)