2014-11-24 60 views
4

我想從圖庫中選擇圖像,然後將此圖像轉換爲文件並通過HttpPost發送它,但我總是得到FileNotFoundException。這是我的代碼:從Uri類型創建文件android

選擇照片

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 

因此,任何他LP?


更新 我曾嘗試這種解決方案也:

//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 itidx VAR ==來-1

而且我已經試過@Praneeth卡魯解決方案,但它返回結果始終爲空。

回答

6
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(); 
    } 
    } 
} 

得到真正的路徑現在modifiy你的代碼像

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(); 
    } 
    } 
} 
+0

我想這已經檢查我的更新吧。 – Chlebta 2014-11-24 11:44:07

+8

你給的函數總是返回null。 – Chlebta 2014-11-24 12:11:05