2012-09-16 69 views
1

我想寫一個簡單的測試方法,從Asset文件夾複製文件到SD卡。當我嘗試打開SD卡上的文件時,它會崩潰。FileOutputStream在嘗試FileOutputStream(externalStorage.getAbsolutePath())時會導致異常;

代碼

 try { 
       // POpne file from asset 
       AssetManager assetManager = getAssets(); 
       InputStream inputStream; 
       inputStream = assetManager.open("test"); 

       // open output folder 
       File externalStorage = Environment.getExternalStorageDirectory(); 

         // CRASHES HERE 
       out = new FileOutputStream( externalStorage.getAbsolutePath() ); 
       int c; 

      while ((c = inputStream.read()) != -1) { 
       out.write(c); 
      } 
     } catch(Exception e) { 

     } 
    } 
+2

你加WRITE_EXTERNAL_STORAGE允許您的應用程序? – auselen

+0

Nop,很容易修復!,感謝您的評論 –

回答

1

看起來你忘了提到在AndroidManifest.xml文件訪問權限,只寫下列權限在AndroidManifest.xml中

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

嗨,不,我忘了,現在就去它,謝謝你的幫助:) –