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 !