2014-01-07 33 views
0

我實現的功能來保存文件,在我的應用程序,它被一些exteral應用程序發送的數據recieving。解決路徑,而從外部應用程序

我已經提供了單一和多張文件的支持。提供了處理所有類型的文件。

,但我不能夠處理以下情形。

我查看從電子郵件客戶端文件 - >查看它在Quickoffice - >點擊發送 - >選擇我的APP->然後點擊保存在我的應用程序。

在我得到

java.io.FileNotFoundException: /file:/data/data/com.qo.android.sp.oem/files/temp/Error.log: open failed: ENOENT (No such file or directory) 

我已經看到了這個帖子是用於處理具有內容方案 Get filename and path from URI from mediastore

下面URI是非常有用的下面包裹着異常的路徑是我的代碼

Uri uri = (Uri) iterator.next(); 
          if ("content".equals(uri.getScheme())) { 
           filePath = getFilePathFromContentUri(uri, hostAcitvity.getContentResolver()); 
          } 
          else { 
           filePath = uri.getPath(); 
          } 
          fileName = uri.getLastPathSegment(); 
          fileSize = hostAcitvity.getContentResolver().openInputStream(uri).available(); 

getFilePathFromContentUri代碼

private String getFilePathFromContentUri(Uri selectedVideoUri, ContentResolver contentResolver) 
    { 
     String filePath; 
     String[] filePathColumn = { MediaColumns.DATA }; 

     Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn, null, null, null); 
     cursor.moveToFirst(); 

     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     filePath = cursor.getString(columnIndex); 
     cursor.close(); 
     return filePath; 
    } 

然後,我把路徑放在FileInputStream中拋出上述異常 無法正確解析文件路徑。這是找到路徑的正確方法嗎?

歡呼聲, Saurav

回答

0

我已經看到了這個帖子是用於處理具有內容方案

這絕不可靠地工作,將在今後的工作中更加不可靠的URI非常有用。

這是尋找路徑的正確方法是什麼?

不,因爲沒有要求每個Uri都映射到您可以訪問的文件系統上的路徑。

使用getInputStream()ContentResolver就要上UriInputStream,和消費數據的方式。

+0

非常感謝馬克...我得到你的觀點,它解決了我的問題 – saurav

+0

只是對內容的變壓器輸入蒸汽add..opening當文件來自應用程序,如文件管理器和不工作的時候文件是在SD卡 – saurav