2012-04-21 44 views

回答

1

在運行時不能將其保存在res/raw中,您可以將其保存在sdcard或緩存目錄中。

爲了將文件保存到SD卡上,您必須有權寫入卡。您需要將以下添加到您的AndroidManifest.xml:

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

接下來你打開一個文件流,寫微博,何地,你想這樣做關閉它,在代碼:

String filename = "filename.txt"; 
File file = new File(Environment.getExternalStorageDirectory(), filename); 
FileOutputStream fos; 
byte[] data = new String("data to write to file").getBytes(); 
try { 
    fos = new FileOutputStream(file); 
    fos.write(data); 
    fos.flush(); 
    fos.close(); 
} catch (FileNotFoundException e) { 
    // handle exception 
} catch (IOException e) { 
    // handle exception 
} 
+0

但是我已經準備好了這些文件,我該如何將它們放入SD卡中。 – Jane 2012-04-21 14:55:08

+0

如果您使用上面的代碼,那麼該文件將在SD卡上 – 2012-04-21 14:57:46

0

如果你希望在你的apk文件裏面,並在運行時讀取它,你應該把它放在assets/文件夾中的項目,你可以使用:

InputStream istream = context.getAssets().open(FILE_NAME); 

去實現它。但是,如果您正在尋找從/ sdcard中讀取文件的方法,請參閱其他答案。