2017-08-07 174 views
-2

這是我第一次製作Android應用程序,所以我對所有內容的條款都有些遺憾。Android:將文件保存到SD卡

我試圖將我在我的/ raw /目錄中的文件複製到我的SD卡的根目錄。

目前,(使用#1,沒有完全寫我自己)的代碼如下所示:

btnWriteSDFile.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View v) 
     { 

      try { 
       File myFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "fileName.docx"); 
       myFile.createNewFile(); 
       Toast.makeText(v.getContext(),"Wrote line", Toast.LENGTH_SHORT).show(); 
       FileOutputStream fOut = new FileOutputStream(myFile); 
       OutputStreamWriter myOutWriter =new OutputStreamWriter(fOut); 
       myOutWriter.append("testFile"); 
       myOutWriter.close(); 
       fOut.close(); 
       Toast.makeText(v.getContext(),"Done writing SD 'mysdfile.txt'", Toast.LENGTH_SHORT).show(); 
      } 
      catch (Exception e) 
      { 
       Toast.makeText(v.getContext(), e.getMessage(),Toast.LENGTH_SHORT).show(); 
      } 
     } 


    }); 

我得到的錯誤: 「打開失敗; EACCES(拒絕)」。

我的清單看起來是這樣的:

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
     <uses-permission android:name="android.permisson.READ_EXTERNAL_STORAGE" /> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

</application> 

澄清,以避免重複: 除了這個事實,我得到這個錯誤:我怎麼能寫一個/raw/file.docx文件到我的SD卡根?

+1

工作除了在<錯誤的地方使用了<使用權限>(per答案),你還需要考慮[運行時權限](https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it)。 – CommonsWare

+1

[異常'打開失敗:EACCES(權限被拒絕)'在Android上可能重複](https://stackoverflow.com/questions/8854359/exception-open-failed-eacces-permission-denied-on-android) –

+0

謝謝,我也會包括這一點!我很想知道,我怎麼把我的/raw/file.docx寫到SD卡上? –

回答

2

根據this文檔中,你不得不在屋裏和直接添加使用權限<manifest>標籤

<manifest> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="android.permisson.READ_EXTERNAL_STORAGE" /> 
    ... 
    <application> 
     ... 
     <activity> 
      ... 
     </activity> 
    </application> 
</manifest> 

這樣

<manifest> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="android.permisson.READ_EXTERNAL_STORAGE" /> 
<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

</application> 
</manifest> 

這應該用於複製文件

InputStream in = getResources().openRawResource(R.raw.fileNameYourGoingToCopy); 
String path = Environment.getExternalStorageDirectory() + "/anyFolder" + File.separator + fileNameWithExtension; 
FileOutputStream out = new FileOutputStream(path); 
byte[] buff = new byte[1024]; 
int read = 0; 
try { 
    while ((read = in.read(buff)) > 0) { 
     out.write(buff, 0, read); 
    } 
} finally { 
    in.close(); 
    out.close(); 
} 
+0

謝謝!這將刪除該位置的錯誤,但是如何將我的/raw/file.docx寫入目錄? –

+0

@clanc我編輯過。檢查它是否有效。這是這個答案的重複。 https://stackoverflow.com/a/19465224/5202960 –

+0

工程就像一個魅力,感謝您的幫助! –