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

时间:2015-06-22 07:37:29

标签: android image filepath

在我的应用程序中,用户可以从相机中选择图片或从图库中选择并裁剪它以发送到服务器。所以没有问题。, 问题是用户从图库中选择图片

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

错误:

  

IOException:/ external / images / media / 19:open failed:ENOENT(不是这样的   文件或目录)

2 个答案:

答案 0 :(得分:2)

您应该从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;
    }

答案 1 :(得分:0)

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

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

在API 19之后,内容URI发生了变化

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

<强> /storage/3437-6630/Pics/IMG_5737.JPG

在API19之前调用图库意图它将导致Gallery.But 在API 19之后它将导致调用图像。从这里您可以访问图库或外部存储或Google云端硬盘。所以有机会获得 RuntimeException

  

MainActivity.java

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

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