Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从Uri类型android创建文件_Java_Android_File_Uri_Loopj - Fatal编程技术网

Java 从Uri类型android创建文件

Java 从Uri类型android创建文件,java,android,file,uri,loopj,Java,Android,File,Uri,Loopj,我正在尝试从gallery中选择图像,然后将此图像转换为文件并通过HttpPost发送,但我总是得到FileNotFoundException。这是我的代码: 选择照片 public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { if (requestCode == 1) {

我正在尝试从gallery中选择图像,然后将此图像转换为文件并通过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
转换照片

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:无法从CursorWindow读取第0行,第1列。确保光标在从中访问数据之前已正确初始化
idx
var==to
-1

我也尝试过@praneth-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();
    }
  }
}

您提供的函数总是返回null。