如何在 Android 中通过 MultiPart 将图像发送到服务器时获取完整路径

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

在我的应用程序中,用户可以从相机或Select从图库拍照并裁剪以发送到服务器.所以没有问题。 问题是当用户 select 图片来自图库

裁剪后的相机 URI 路径: /mnt/sdcard/avatar_1434958340804.jpg
来自存储的 URI 路径被裁剪: /external/images/media/19

错误:

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

您应该从 URI 获取路径。使用以下功能:

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;
    }

裁剪后的相机 URI 路径:/mnt/sdcard/avatar_1434958340804.jpg

来自存储的 URI 路径被裁剪:/external/images/media/19

在 API 19 之后,内容 URI 已更改

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

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

在 API19 之前调用图库 Intent 会导致 Gallery.But 在 API19 之后会导致调用图片.从这里您可以转到图库或外部存储或 Google 驱动器。所以有机会获得 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;
}