Created
June 17, 2014 23:02
-
-
Save laaptu/a9d13ba55ca3c5a68f31 to your computer and use it in GitHub Desktop.
Method to get image path from Camera Intent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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