1

/0失敗,我想從一個位置在設備上的文件複製到應用程序文件夾,並使用:複製文件從/根/存儲/模擬在Android

public static void copyFile(final String srcAbsolutePath, final String dstAbsolutePath) throws IOException { 
     FileInputStream srcFileStream = null; 
     FileOutputStream dstFileStream = null; 
     try { 
      srcFileStream = new FileInputStream(srcAbsolutePath); 
      dstFileStream = new FileOutputStream(dstAbsolutePath); 
      byte[] writeBytes = new byte[1024]; 

      int bytesCount; 
      while ((bytesCount = srcFileStream.read(writeBytes)) > 0) { 
       dstFileStream.write(writeBytes, 0, bytesCount); 
      } 
     } catch (IOException ex) { 
      Log.e(TAG, "Failed to copy the file from src="+srcAbsolutePath+" to="+dstAbsolutePath, ex); 
      throw ex; 
     } finally { 
      // close the streams 
      if (srcFileStream != null) { 
       srcFileStream.close(); 
      } 

      if (dstFileStream != null) { 
       dstFileStream.close(); 
      } 
     } 
    } 

,我遇到了此錯誤:

Failed to copy the file from src=/root/storage/emulated/0/TestApp/1a5e67e1-c166-4a52-abb4-3de61898e109.pdf to=/data/user/0/com.myapp.package/files/78e6a56b-3141-4024-a3a1-f3f44ccc6dfb.pdf 
                     java.io.FileNotFoundException: /root/storage/emulated/0/TestApp/1a5e67e1-c166-4a52-abb4-3de61898e109.pdf (Permission denied) 

我有以下權限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

我使用https://github.com/spacecowboy/NoNonsense-FilePicker來挑選文件(Uri)。

我在AndroidManifest.xml

<provider 
     android:name="android.support.v4.content.FileProvider" 
     android:authorities="${applicationId}.provider" 
     android:exported="false" 
     android:grantUriPermissions="true"> 
     <meta-data 
      android:name="android.support.FILE_PROVIDER_PATHS" 
      android:resource="@xml/nnf_provider_paths" /> 
    </provider> 

下如何修正這個錯誤?

+0

嗯,這應該是大多數Android設備上無效的路徑,所以我並不感到驚訝,它崩潰。你需要弄清楚圖書館爲什麼給你這個價值。儘管你的問題中沒有任何代碼與此有關。 – CommonsWare

+0

確保文件劑量存在,你有'java.io.FileNotFoundException'警告 – Alamri

回答

相關問題