2017-04-04 189 views
0

沒有這樣的文件或目錄當試圖從路徑

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
         intent.setType("file/*"); 
         intent.setType("image/*"); 
         intent.setType("video/*"); 
         // intent.addCategory(Intent.CATEGORY_OPENABLE); 
         startActivityForResult(intent, READ_REQUEST_CODE) 
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) { 
      // The document selected by the user won't be returned in the intent. 
      // Instead, a URI to that document will be contained in the return intent 
      // provided to this method as a parameter. 
      // Pull that URI using resultData.getData(). 
      Uri uri = null; 

      if (resultData != null) { 
       uri = resultData.getData(); 
       uri1 = uri; 
       Log.i("anderwidth", "Uri: " + uri.toString()); 
File file = new File(uri1.getPath()); 
      } 
     } 

我使用改裝與Android閱讀。那是什麼發生?我無法讀取圖像和視頻作爲文件,file.exists給我false.Everytime其內容// ...

它的工作原理,如果有人下載的文件管理器應用程序,而不是如果沒有下載。

+0

'File file = new File(uri1.getPath());'。這是行不通的。請發佈uri1.getPath()和uri2.toString()的完整值。 – greenapps

回答

1

如果您想從路徑「讀」,然後打開一個InputStream和從流中讀取()。

InputStream is = getContentResolver().openInputStream(uri); 
+0

我怎樣才能把它轉換成文件?因爲我需要上傳文件? – Zenzen

+0

那麼如果你有文件系統路徑並打開了FileInputStream,你會怎麼做?您將讀取()來自該流的字節。那麼如果你自己做了。如果你使用庫,那麼讓庫使用uri。 – greenapps

相關問題