如何在应用程序中添加SD卡文件?

时间:2012-09-02 17:13:25

标签: android file apk sd-card

我需要在我的应用程序中添加一些文件,这些文件必须在应用程序安装后解压缩到SD卡。在Android上有可能吗?我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

将文件添加到资源/原始目录中。应用程序运行时,您可以检查目标文件是否存在。如果它不存在,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();
}