简体   繁体   English

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

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

I need add some files in my application and this files must be unpacked to sd card after application install. 我需要在我的应用程序中添加一些文件,这些文件必须在应用程序安装后解压缩到SD卡。 Is it possible on Android? 在Android上有可能吗? How I can to do that? 我怎么能这样做?

Add the file to your resources/raw directory. 将文件添加到资源/原始目录。 When the application runs, you can check for the existence of the target file. 应用程序运行时,您可以检查目标文件是否存在。 If it doesn't exist, unpack and write to the SD card : 如果它不存在,请解压缩并写入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