how to get Full Path if image to send to server by MultiPart in Android

1.3k Views Asked by At

In My App, user can take Picture from Camera Or Select from Gallery And Crop It to send to server. So there is No Problem., Problem is when user select picture from Gallery

URI path from camera cropped : /mnt/sdcard/avatar_1434958340804.jpg
URI path from Storage cropped : /external/images/media/19

Error:

IOException : /external/images/media/19: open failed: ENOENT (No such file or directory)

2

There are 2 best solutions below

1
On BEST ANSWER

You should get path from URI. Use below function:

private String getRealPathFromURI(Uri contentURI) {
        //Log.e("in","conversion"+contentURI.getPath());
       String path;
        Cursor cursor = getContentResolver()
                   .query(contentURI, null, null, null, null); 
        if (cursor == null) 
            path=contentURI.getPath();

        else {
            cursor.moveToFirst(); 
            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
            path=cursor.getString(idx);

        }
        if(cursor!=null)
            cursor.close();
        return path;
    }
0
On

URI path from camera cropped : /mnt/sdcard/avatar_1434958340804.jpg

URI path from Storage cropped : /external/images/media/19

After API 19 the content URI changed

content://com.android.providers.media.documents/document/image%3A151323

and

/storage/3437-6630/Pics/IMG_5737.JPG

Before API19 the Calling the gallery Intent It'll Lead to Gallery.But After API 19 it will lead to call images. From here you can go to Gallery or External Storage or Google drive. so there is chance to get a RuntimeException.

MainActivity.java

       String realPath =" ";
       try {
            realPath = RealPathUtil.getRealPathFromURI(this, data.getData());
        }catch (RuntimeException e){
            realPath = RealPathUtil.getRealPathFromURI_API19(this, data.getData());
        }

In RealPathUtil.java

getRealPathFromURI()

public static String getRealPathFromURI(Context context,Uri contentURI) {
    //Log.e("in","conversion"+contentURI.getPath());
    String path;
    Cursor cursor = context.getContentResolver()
            .query(contentURI, null, null, null, null);
    if (cursor == null)
        path=contentURI.getPath();

    else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        path=cursor.getString(idx);

    }
    if(cursor!=null)
        cursor.close();
    Log.e("GGGG CCC ",path);
    return path;
}

getRealPathFromURI_API19

@SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri){
    String filePath = "";
    String wholeID = DocumentsContract.getDocumentId(uri);

    // Split at colon, use second item in the array
    String id = wholeID.split(":")[1];

    String[] column = { MediaStore.Images.Media.DATA };

    // where id is equal to
    String sel = MediaStore.Images.Media._ID + "=?";

    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            column, sel, new String[]{ id }, null);

    int columnIndex = 0;
    if (cursor != null) {
        columnIndex = cursor.getColumnIndex(column[0]);
    }

    if (cursor != null && cursor.moveToFirst()) {
        filePath = cursor.getString(columnIndex);
    }
    if (cursor != null) {
        cursor.close();
    }
    Log.e("GGGGG FilePath",filePath);
    return filePath;
}