Skip to content

Instantly share code, notes, and snippets.

Last active December 20, 2015 00:59
Show Gist options
  • Save jvergeldedios/6046027 to your computer and use it in GitHub Desktop.
Save jvergeldedios/6046027 to your computer and use it in GitHub Desktop.
public static final String PREFS_NAME = "MyPrefsFile";
public static final String IMAGE_TEMP_PATH = "ImageTempPath";
public static final int REQUEST_CAMERA = 1;
protected void launchCamera()
// Setup a temporary storage path for the image and save it in SharedPreferences
String imageTempPath = "image_" + UUID.randomUUID() + ".jpg";
getSharedPreferences(PREFS_NAME, 0).edit().putString(IMAGE_TEMP_PATH, imageTempPath).commit();
File dir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
Uri dest = Uri.fromFile(new File(dir, imageTempPath));
// Pass that path to the camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, dest);
startActivityForResult(intent, REQUEST_CAMERA);
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if(resultCode != RESULT_OK)
if(requestCode == REQUEST_CAMERA)
// Since we saved the path to the image, we can retrieve it and use it for whatever we want
Intent intent = new Intent();
File dir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File f = new File(dir, getSharedPreferences(PREFS_NAME, 0).getString(IMAGE_TEMP_PATH, ""));
Uri uri = Uri.fromFile(f);
intent.putExtra("image-path", uri.toString());
super.onActivityResult(requestCode, resultCode, data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment