java - 从 Uri 类型 android 创建文件

标签 java android file uri loopj

我尝试从图库中选择图像,然后将此图像转换为文件并通过 HttpPost 发送,但我总是遇到 FileNotFoundException。这是我的代码:

选择照片

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 1) {
                // currImageURI is the global variable I’m using to hold the content:
                currImageURI = data.getData();
                //Save the currImageUri (URI type) to global variable.
                photosHolder.getInstance().setOneIm(currImageURI);
            }
        }
    }

正在转换照片

                // First Try
                File myFile = new File((photosHolder.getInstance().getOneIm()).toString());
                params.put("visit_report[photos_attributes][0][file]",myFile); **The exception Raised here
// second try 
                File myFile2 = new File((photosHolder.getInstance().getOneIm()).getPath());
                params.put("visit_report[photos_attributes][1][file]",myFile2); ** Also here

那些是调试时的 myFiles 值:

mFile : content:/com.android.providers.media.documents/document/image%3A19143
myFile2 : /document/image:19143

那么有什么帮助吗?


更新 我也尝试过这个解决方案:

//get The real path from uri then save it (and then use it to create the file)
photosHolder.getInstance().setUriString(getRealPathFromURI(currImageURI));

//Convert the image URI to the direct file system path of the image file
    private String getRealPathFromURI(Uri contentURI) {
        String result = "";
        try {
        Cursor cursor = getActivity().getContentResolver().query(contentURI, null, null, null, null);
        if (cursor == null) { // Source is Dropbox or other similar local file path
            result = contentURI.getPath();
        } else {
            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
            result = cursor.getString(idx); // Exception raised HERE
            cursor.close(); }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

但是我得到了 java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow。确保 Cursor 在访问数据之前正确初始化 并且 idx var == to -1

我也尝试过@Praneeth Kalluri 解决方案,但结果总是返回 null。

最佳答案

 Uri currImageURI = data.getData();

打印 currImageURI 会给你这样的东西:

content://media/external/images/media/47

但我们需要的是该特定图像的绝对路径。所以我们需要从uri获取真实路径

public String getRealPathFromURI(Context context, Uri contentUri) {
  Cursor cursor = null;
  try { 
    String[] proj = { MediaStore.Images.Media.DATA };
    cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
  } finally {
    if (cursor != null) {
      cursor.close();
    }
  }
}

现在修改你的代码

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 1) {
                // currImageURI is the global variable I’m using to hold the content:
                currImageURI = data.getData();
                //Save the currImageUri (URI type) to global variable.
                photosHolder.getInstance().setOneIm(getRealPathFromURI(getActivity(),currImageURI));
            }
        }
    }

public String getRealPathFromURI(Context context, Uri contentUri) {
  Cursor cursor = null;
  try { 
    String[] proj = { MediaStore.Images.Media.DATA };
    cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
  } finally {
    if (cursor != null) {
      cursor.close();
    }
  }
}

关于java - 从 Uri 类型 android 创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27103529/

相关文章:

android - 在 google-glass 和 android 设备之间交换数据

C# - 如何解析文本文件(空格分隔的数字)?

php - 文件大小(): giving incorrect result

java - 重定向到 URL 时无法访问 SpringMVC Controller 中的请求参数

java - 在 spring boot 应用程序上增加 max-http-header-size 是否有危险/影响?

android - 如何在 fragment 事务期间停止异步任务

android - 我移动时如何绘制根路径?

java - java访问文件时出现错误

java - 生成的 XML 中的子元素没有父级的命名空间 - JAXB

java - 如何使用 Apache Commons IO 以自定义名称复制 java 中的文件?