2017-01-27 82 views
0

選擇視頻我都 /文件/視頻:5312 和 內容://com.android.providers.media.documents/document/video%3A5312文件沒有找到,從畫廊

作爲我從圖庫中選擇的視頻文件的「URI」或「絕對路徑」,並將其饋送到新的File();

File selectedFile = new File(selectedFilePath); 

我已經嘗試了上部URI /路徑,他們只是不工作。 是否不是工作。該文件是找不到

什麼格式進行文件()要求? 此路徑有效。我如何用File()打開它?

+0

你的問題是如此含糊 –

+0

編輯有點..好? –

+0

我其實沒有得到你想要的東西? –

回答

2

「URI」或我是從庫

選擇了「絕對路徑」爲視頻文件那些是Uri值。它們不是文件系統路徑,也不能爲它們獲取文件系統路徑。

該文件未找到。

那是因爲他們沒有文件。 content://com.android.providers.media.documents/document/video%3A5312的計劃爲content,而不是file

類似地,https://stackoverflow.com/questions/41900707/file-not-found-selecting-a-video-from-gallery不是文件,因爲Uri具有https的方案。

如何使用文件打開它()?

你不會比你使用File打開https://stackoverflow.com/questions/41900707/file-not-found-selecting-a-video-from-gallery更多。

相反,you use ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri,就像你可能會使用OkHttp或HttpUrlConnection到上httpsUri得到InputStream

+0

您可以幫助我使用此方法上傳文件的示例...?我只在谷歌上找到一個Xamarin。 –

+0

@DavidKachlon:好的,很少有HTTP客戶端API純粹使用'InputStream'工作,以防他們需要重新啓動HTTP操作。您必須查看您選擇的HTTP API是否支持'InputStream'(例如,OkHttp不支持)。如果確實如此,那很好。如果沒有,也可以在某個地方打開一個'FileOutputStream'(例如,'getCacheDir()'),並將InputStream中的字節複製到FileOutputStream中。然後,您可以將您的內容副本作爲文件使用,並在完成後將其刪除。 – CommonsWare

0

確定。謝謝。

這裏是我的管理....

String[] perms = {"android.permission. WRITE_EXTERNAL_STORAGE"}; 

    int permsRequestCode = 200; 

    requestPermissions(perms, permsRequestCode); 



    public void convertFile(Uri urx) 
     throws IOException { 

    ContentResolver test = getContentResolver(); 
    InputStream initialStream = test.openInputStream(urx); 


    byte[] buffer = new byte[initialStream.available()]; 
    initialStream.read(buffer); 

    File xpath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES); 


    File targetFile = new File(xpath, "/test.mp4"); 
    OutputStream outStream = new FileOutputStream(targetFile); 
    outStream.write(buffer); 
    uploadFile(targetFile.getPath()); 

} 

@Override 
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){ 

    switch(permsRequestCode){ 

     case 200: 

      boolean writeAccepted = grantResults[0]== PackageManager.PERMISSION_GRANTED; 

      break; 

    } 

} 

我在試圖寫我的MP4文件得到一個EACES錯誤。有沒有辦法「將它寫入內存」?或者,也許你可以解決這個問題。