簡體   English   中英

從Uri類型android創建文件

[英]Create File from Uri type android

我正在嘗試從庫中選擇圖像然后將此圖像轉換為文件並通過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 Value:

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. Make sure the Cursor is initialized correctly before accessing data from it java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from itidx var ==到-1 java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it

我也試過@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();
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM