裁剪活动的保存操作会导致应用程序库的ANR

时间:2015-06-28 14:23:37

标签: android

我编写了一个带有按钮的应用程序来调用活动以使用方法startActivityForResult拍照。调用onActivityResult后,应用程序会转到裁剪活动,以使用方法startActivityForResult裁剪刚拍摄的图像。但是当我保存它时,图库应用程序总是停止,返回应用程序未响应错误,这导致onActivityResult方法中的参数resultCode为 0 ,即RESULT_CANCELED。< / p>

代码只是onCreate()onActivityResult()和以下变量:

public static final int TAKE_PHOTO = 1;

public static final int CROP_PHOTO = 2;

private Button takePhoto;

private ImageView picture;

private Uri imageUri;

的onCreate():

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    takePhoto = (Button) findViewById(R.id.take_photo);
    picture = (ImageView) findViewById(R.id.picture);
    takePhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //init File object, to save the image
            File outputImage = new File(Environment.getExternalStorageDirectory(), "output_image.jpg");
            try{
                if(outputImage.exists()){
                    outputImage.delete();
                }
                outputImage.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            imageUri = Uri.fromFile(outputImage);

            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            startActivityForResult(intent, TAKE_PHOTO);
        }
    });

onActivityResult():

switch (requestCode){
        case TAKE_PHOTO:
            if(resultCode == RESULT_OK){
                Intent intent = new Intent("com.android.camera.action.CROP");
                intent.setDataAndType(imageUri, "image/*");
                intent.putExtra("scale", true);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(intent, CROP_PHOTO);
            }
            break;

        case CROP_PHOTO:
            Log.d("MainActivity", "CROP_PHOTO_IN, result code : " + resultCode);
            if(resultCode == RESULT_OK){
                Log.d("MainActivity", "CROP_PHOTO_OK");
                try {
                    Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                    picture.setImageBitmap(bitmap);
                    Log.d("MainActivity", "PHOTO_SET");
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
            break;

        default:
            break;
    }

相关日志:

06-28 10:30:35.750 1682-1694 /? W / CameraCaptureSession:会话0:相机设备已关闭:     java.lang.IllegalStateException:CameraDevice已经关闭 在android.hardware.camera2.impl.CameraDeviceImpl.checkIfCameraClosedOrInError(CameraDeviceImpl.java:1482)             在android.hardware.camera2.impl.CameraDeviceImpl.stopRepeating(CameraDeviceImpl.java:677)             在android.hardware.camera2.impl.CameraCaptureSessionImpl.close(CameraCaptureSessionImpl.java:328)             在android.hardware.camera2.impl.CameraCaptureSessionImpl.finalize(CameraCaptureSessionImpl.java:561)             在java.lang.Daemons $ FinalizerDaemon.doFinalize(Daemons.java:191)             在java.lang.Daemons $ FinalizerDaemon.run(Daemons.java:174)             在java.lang.Thread.run(Thread.java:818) 06-28 10:30:38.200 483-561 /? W / AudioTrack:客户拒绝了AUDIO_OUTPUT_FLAG_FAST 06-28 10:30:38.205 1589-1782 /? D / skia:--- SkImageDecoder :: Factory返回null 06-28 10:30:38.205 1589-1782 /? W / CropActivity:无法为文件打开区域解码器:file:///storage/emulated/0/output_image.jpg     java.io.IOException:不支持图像格式             在android.graphics.BitmapRegionDecoder.nativeNewInstance(本机方法)             在android.graphics.BitmapRegionDecoder.newInstance(BitmapRegionDecoder.java:124)             在com.android.gallery3d.filtershow.crop.CropActivity $ BitmapIOTask.doInBackground(CropActivity.java:483)             在com.android.gallery3d.filtershow.crop.CropActivity $ BitmapIOTask.doInBackground(CropActivity.java:355)             在android.os.AsyncTask $ 2.call(AsyncTask.java:292)             在java.util.concurrent.FutureTask.run(FutureTask.java:237)             在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)             at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587)             在java.lang.Thread.run(Thread.java:818) 06-28 10:30:38.206 1589-1782 /? D / skia:--- SkImageDecoder :: Factory返回null

此外,有时候这种情况并没有发生,对我来说这是不可预测的。 但它始终导致FileNotFoundException由权限拒绝引起。 我添加了使用权限,包括读取和写入,不在应用程序标签内。它没有帮助。

0 个答案:

没有答案