I am working on an android application. It has a functionality that captured image should display on screen and I need to get that image path, so that I can send that image to server.
I am able to display image on screen, but unable to get absolute image path.
I am getting path like:
content://media/external/images/media/1220
content://media/external/images/media/1221
How can I get actual image path?
Here is my code:
mintent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
mintent.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
startActivityForResult(mintent, 1);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((data != null && !data.toString().equalsIgnoreCase("Intent { }"))
|| requestCode == 1)
switch (requestCode) {
case 1:
try {
// Uri imageFileUri = data.getData();
// String path=getRealPathFromURI(this,imageFileUri);
// Bitmap bitmap = (Bitmap) data.getExtras().get("data");
String photoPath = null;
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
Intent intent = new Intent(this, Media.class);
intent.putExtra("BitmapImage", bitmap);
Bundle b = mintent.getExtras();
if (b != null && b.containsKey(MediaStore.EXTRA_OUTPUT)) { // large
// image?
String timeStamp = new SimpleDateFormat(
"yyyyMMdd_HHmmss").format(new Date());
// Shouldn't have to do this ... but
photoPath = MediaStore.Images.Media.insertImage(
getContentResolver(), bitmap, "Img" + timeStamp
+ ".jpeg", null);
}
}
}
}
photoPath
value is shown in above.
How can I get my absolute image path from this path.
This is a working function--
String selectedImagePath = getPath(selectedImageUri);
private String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null,null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
A complete working solution
final private int PICK_IMAGE = 1;
final private int CAPTURE_IMAGE = 2;
private String imgPath;
btnGallery.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);
}
});
btnCapture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
startActivityForResult(intent, CAPTURE_IMAGE);
}
});
public Uri setImageUri() {
// Store image in dcim
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png");
Uri imgUri = Uri.fromFile(file);
this.imgPath = file.getAbsolutePath();
return imgUri;
}
public String getImagePath() {
return imgPath;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_CANCELED) {
if (requestCode == PICK_IMAGE) {
selectedImagePath = getAbsolutePath(data.getData());
imgUser.setImageBitmap(decodeFile(selectedImagePath));
} else if (requestCode == CAPTURE_IMAGE) {
selectedImagePath = getImagePath();
imgUser.setImageBitmap(decodeFile(selectedImagePath));
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
public String getAbsolutePath(Uri uri) {
String[] projection = { MediaColumns.DATA };
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}
public Bitmap decodeFile(String path) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 70;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeFile(path, o2);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
on camera button
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, CAMERA_REQUEST);
} catch (ActivityNotFoundException e) {
// Do nothing for now
}
and on activity result write
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
// Bitmap photo1 = (Bitmap) data.getExtras().get("data");
File file1 = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
Bitmap bitmap11 = decodeSampledBitmapFromFile(file1.getAbsolutePath(), 500, 300);
//
}
}