繁体   English   中英

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

[英]How to add sd-card's files in app?

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

将文件添加到资源/原始目录。 应用程序运行时,您可以检查目标文件是否存在。 如果它不存在,请解压缩并写入SD卡

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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM