1

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 !

Jonsmoke
  • 822
  • 9
  • 18
  • If you remove the code from onActivityResult(), does it crash then? – greenapps Sep 16 '14 at 09:18
  • If you start horizontal before you start the camara, what happens? – greenapps Sep 16 '14 at 09:20
  • If i start horizontal, then i have to stick to horizontal, but it works, and will crash if i switch to portrait ! – Jonsmoke Sep 16 '14 at 09:26
  • @bumblebeez Is `dispatchTakePictureIntent()` the only place you do anything with the actual process of taking pictures? If that's the case, then it might have more to do with how you're saving the image rather than the image taking orientation. – erad Sep 16 '14 at 09:45
  • @erad i also use a method to add the picture to the phone gallery, i edit my code to post it, but yes this is the only place where i call a picture intent, and i precise that everything works just fine, except when i swith orientation ! – Jonsmoke Sep 16 '14 at 09:49
  • Repeat: `If you remove the code from onActivityResult(), does it crash then?` It will crash on `galleryAddPic();`. – greenapps Sep 16 '14 at 09:53
  • @greenapps sorry i could not try this before, no it does not crash if i remove the onActivityResult method – Jonsmoke Sep 16 '14 at 10:00
  • If it does not crash when you remove `onActivityResult`, then your problem is probably with how you're saving the image. – erad Sep 16 '14 at 10:02
  • As said earlier it crashes on `galleryAddPic();` as you have setup something before invoking the camera. This will have gone during orientation change. Probably the used filename. – greenapps Sep 16 '14 at 10:08
  • That was the problem ! i found out that my variables were reinitialized when i switched orientation, i did not knew that (that's my first real app) i will have to find out a way to keep datas when i swith orientation ! – Jonsmoke Sep 16 '14 at 10:09
  • Post your error trace. – Siddharth_Vyas Sep 16 '14 at 10:21

1 Answers1

1

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();
        }
    }
erad
  • 1,766
  • 2
  • 17
  • 26
  • thank you, but i solved my problem using Bundles with onSaveInstanceState() and onRestoreInstanceState() methods ! – Jonsmoke Sep 16 '14 at 13:38