Application crashes when taking photos successively in portrait then in landscape mode

655 Views Asked by At

I have a feature in my android application which allow the user to take photos and then save them in a little gallery inside the app.
Everything works fine when i start taking photo in portrait mode and then stick to portrait mode, but when i decide to switch to landscape mode, i can take the photo but when i click on the confirmation button (the moment when you decide to keep continuing with the actual photo or go back to take another one) the application then crashes. Unfortunately i cannot post the Logs right now, but here is the code (which is fully based on the code you can find on android website) :

This is where i create camera intent, the file and save it :

private File createImageFile() throws IOException {

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "UNICAR_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
        imageFileName,  //-/ prefix
        ".jpg",         //-/ suffix
        storageDir      //-/ directory
    );

    SaisieMission activity = (SaisieMission) getActivity();
    activity.setCurrentPhotoPath("file:" + image.getAbsolutePath());
    return image;
}

protected void dispatchTakePictureIntent() {

    Context context = getActivity();
    PackageManager packageManager = context.getPackageManager();
    if (((SaisieMission) getActivity()).getNoPath() < 10) {

        photoPaths = mission.getPaths();
        if(packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA) == false) {
            Toast.makeText(getActivity(), "xxx", Toast.LENGTH_SHORT)
                    .show();
            return ;
        }

        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        SaisieMission activity = (SaisieMission) getActivity();
        if (takePictureIntent.resolveActivity(context.getPackageManager()) != null) {


            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {

                Toast toast = Toast.makeText(context, "xxx", Toast.LENGTH_SHORT);
                toast.show();
            }

            if (photoFile != null) {

                Uri fileUri = Uri.fromFile(photoFile);
                activity.setCapturedImageURI(fileUri);
                activity.setCurrentPhotoPath(fileUri.getPath());
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                activity.getCapturedImageURI());
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    } else {
        Toast.makeText(getActivity(), getString(R.string.enough_photo), Toast.LENGTH_SHORT)
        .show();    
        return ;
    }
}

And this the the onActivityResult() method :

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == Activity.RESULT_OK) {

        SaisieMission saisiemission = (SaisieMission) getActivity();
        galleryAddPic();
        photoPaths[saisiemission.getNoPath()] = saisiemission.getCurrentPhotoPath();
        saisiemission.incNoPath();
        mission.setPaths(photoPaths);

    } else {
        Toast.makeText(getActivity(), "xxx", Toast.LENGTH_SHORT)
        .show();
        return ;
    }
}  

Here is where i put the image into the gallery :

private void galleryAddPic() {

    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    SaisieMission activity = (SaisieMission) getActivity();
    File f = new File(activity.getCurrentPhotoPath());
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.getActivity().sendBroadcast(mediaScanIntent);
}  

I also precise that i call all of these methods into a fragment

Thank you for reading !

1

There are 1 best solutions below

1
On

Try something like this: Pass the specific image filename as a argument to your Intent for capture image as putExtra parameter. Insert this image Uri in Media Store and now you can use this Uri for your other things. You can check whether image is captured or not by File.exist()

Ex:

ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "Image File name");
Uri mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(intentPicture,ACTION_TAKE_PICTURE);

in onActivityResult() add this:

selectedImagePath = getRealPathFromURI(mCapturedImageURI);

Create another method getRealPathFromURI()

//----------------------------------------
    /**
     * This method is used to get real path of file from from uri
     * 
     * @param contentUri
     * @return String
     */
    //----------------------------------------
    public String getRealPathFromURI(Uri contentUri)
    {
        try
        {
            String[] proj = {MediaStore.Images.Media.DATA};
            Cursor cursor = managedQuery(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        catch (Exception e)
        {
            return contentUri.getPath();
        }
    }