Skip to content

Instantly share code, notes, and snippets.

@laaptu
Created June 17, 2014 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laaptu/a9d13ba55ca3c5a68f31 to your computer and use it in GitHub Desktop.
Save laaptu/a9d13ba55ca3c5a68f31 to your computer and use it in GitHub Desktop.
Method to get image path from Camera Intent
Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, Media.DATE_ADDED, null, "date_added ASC");
if(cursor != null && cursor.moveToFirst())
{
do {
uri = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA)));
photoPath = uri.toString();
}while(cursor.moveToNext());
cursor.close();
}
//OR
public String getPathFromUri(Uri contentUri) {
String imagePath;
try {
Cursor cursor = getContentResolver().query(contentUri, null, null,
null, null);
int index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
imagePath = cursor.getString(index);
cursor.close();
// Log.i("imagePath", imagePath);
return imagePath;
} catch (Exception e) {
return null;
}
}
//where we have to pass the contentUri on Image Capture Action
private void initImageUri() {
ContentValues cv = new ContentValues();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat,
Locale.ENGLISH);
String name = IMAGE_START_NAME
+ simpleDateFormat.format(Calendar.getInstance().getTime())
+ "_" + new Random().nextInt(100) + ".jpg";
cv.put(MediaStore.Images.Media.TITLE, name);
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cv);
}
private void clickPicturesThroughCamera() {
try {
initImageUri();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, RETURN_FROM_CAMERA);
} catch (Exception e) {
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println("onActivityResult");
// System.out.println(imageUri.getPath());
if (resultCode == Activity.RESULT_OK) {
if (requestCode == RETURN_FROM_CAMERA) {
if (data != null && data.getData() != null)
imageUri = data.getData();
String filePath = getPathFromUri1();
//OR
filePath=getPathFromUri(imageUri);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment