1

i am getting below type of uri:

 /mnt/sdcard/Pictures/WW/ww_1360248819300.jpg

how to convert above type of URI to below URI:

 content://media/external/images/media/12

please help Thanks

Nilesh Verma
  • 914
  • 1
  • 11
  • 26
  • plz see [Get content uri from file path in android](http://stackoverflow.com/questions/3004713/get-content-uri-from-file-path-in-android) post maybe help u – ρяσѕρєя K Feb 07 '13 at 15:23

1 Answers1

2
public static Uri getImageContentUri(Context context, File imageFile) {
    String filePath = imageFile.getAbsolutePath();
    Cursor cursor = context.getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            new String[] { MediaStore.Images.Media._ID },
            MediaStore.Images.Media.DATA + "=? ",
            new String[] { filePath }, null);
    if (cursor != null && cursor.moveToFirst()) {
        int id = cursor.getInt(cursor
                .getColumnIndex(MediaStore.MediaColumns._ID));
        Uri baseUri = Uri.parse("content://media/external/images/media");
        return Uri.withAppendedPath(baseUri, "" + id);
    } else {
        if (imageFile.exists()) {
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.DATA, filePath);
            return context.getContentResolver().insert(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        } else {
            return null;
        }
    }
}
Nilesh Verma
  • 914
  • 1
  • 11
  • 26