0
我需要在我的應用程序中添加一些文件,並且在安裝應用程序之後,必須將這些文件解壓縮到SD卡。在Android上可以嗎?我該如何做到這一點?如何在應用中添加sd-card的文件?
我需要在我的應用程序中添加一些文件,並且在安裝應用程序之後,必須將這些文件解壓縮到SD卡。在Android上可以嗎?我該如何做到這一點?如何在應用中添加sd-card的文件?
將文件添加到您的資源/原始目錄。當應用程序運行時,您可以檢查目標文件的存在。如果不存在,請不要寫在存儲根目錄下!unpack and write to the SD card:
File dest = Environment.getExternalStorageDirectory();
InputStream in = context.getResources().openRawResource(R.raw.file);
// Used the File-constructor
OutputStream out = new FileOutputStream(new File(dest, "file.zip"));
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
try {
// A little more explicit
while ((len = in.read(buf, 0, buf.length)) != -1){
out.write(buf, 0, len);
}
} finally {
// Ensure the Streams are closed:
in.close();
out.close();
}
請不要寫在存儲根目錄下!使用:/ Android/data//files /請參閱:http://developer.android.com/guide/topics/data/data-storage.html#filesExternal –