Mulgard February 2016

Camera Result always returns RESULT_CANCELED

I want to take pictures with my camera. I just need them temporally so i do the following:

private void takePicture() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (intent.resolveActivity(this.getActivity().getPackageManager()) != null) {
        try {
            this.imageFile = File.createTempFile("CROP_SHOT", ".jpg", this.getActivity().getCacheDir());

            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(this.imageFile));
            intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

            this.startActivityForResult(intent, Globals.REQUEST_TAKE_PHOTO);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Unfortunately the result code RESULT_CANCELED in my onActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.v("PictureEdit", "onActivityResult");

    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == Activity.RESULT_OK) {
        if(requestCode == Globals.REQUEST_TAKE_PHOTO) {
            Log.v("PictureEdit", "onActivityResult take photo");

            this.startCropImage(Uri.fromFile(this.imageFile));
        } else if (requestCode == Globals.REQUEST_PICK_PHOTO) {
            Log.v("PictureEdit", "onActivityResult pick photo");

            this.startCropImage(data.getData());
        } else if (requestCode == Globals.REQUEST_CROP_PHOTO) {
            Log.v("PictureEdit", "onActivityResult crop photo");

            this.imageEncoded = data.getStringExtra(Globals.KEY_IMAGE_ENCODED);

            this.imageView.setImageBitmap(Images.decodeImage(imageEncoded));
            this.buttonSave.setEnabled(true);
        } else {
            Log.v("PictureEdit", "onActivityResult requestCode: " + requestCode + ", result: " + resultCode);
        }
    } else {
        Log.v("PictureEdit", "onActivityResult resultCode is not ok:        

Answers


CommonsWare February 2016

I use the cache directory for creating a temp file:

First, a third-party camera app has no rights to work with your app's private cache directory on internal storage.

Second, you are not supposed to create the file. The camera app is supposed to create the file. Some camera apps may get confused and refuse to overwrite your file.

So, create a File object pointing to a location on external storage (e.g., getExternalCacheDir()), but do not create the file yet. Use that File object as the basis for your EXTRA_OUTPUT value.

Also note that MediaStore.EXTRA_VIDEO_QUALITY is for recording videos; it has no meaning for ACTION_IMAGE_CAPTURE.

Post Status

Asked in February 2016
Viewed 2,983 times
Voted 14
Answered 1 times

Search




Leave an answer