android把raw文件复制到sd,Android复制res/raw目录的文件到SD卡下
发布日期:2021-06-27 04:25:00 浏览次数:2 分类:技术文章

本文共 1206 字,大约阅读时间需要 4 分钟。

private static final String SEPARATOR = File.separator;//路径分隔符

/**

* 复制res/raw中的文件到指定目录

* @param context 上下文

* @param id 资源ID

* @param fileName 文件名

* @param storagePath 目标文件夹的路径

*/

public static void copyFilesFromRaw(Context context, int id, String fileName,String storagePath){

InputStream inputStream=context.getResources().openRawResource(id);

File file = new File(storagePath);

if (!file.exists()) {//如果文件夹不存在,则创建新的文件夹

file.mkdirs();

}

readInputStream(storagePath + SEPARATOR + fileName, inputStream);

}

/**

* 读取输入流中的数据写入输出流

*

* @param storagePath 目标文件路径

* @param inputStream 输入流

*/

public static void readInputStream(String storagePath, InputStream inputStream) {

File file = new File(storagePath);

try {

if (!file.exists()) {

// 1.建立通道对象

FileOutputStream fos = new FileOutputStream(file);

// 2.定义存储空间

byte[] buffer = new byte[inputStream.available()];

// 3.开始读文件

int lenght = 0;

while ((lenght = inputStream.read(buffer)) != -1) {// 循环从输入流读取buffer字节

// 将Buffer中的数据写到outputStream对象中

fos.write(buffer, 0, lenght);

}

fos.flush();// 刷新缓冲区

// 4.关闭流

fos.close();

inputStream.close();

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

在需要的地方调用以上源码

转载地址:https://blog.csdn.net/weixin_42166105/article/details/117586027 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:signature=dedda52028ba9cc6699359258f4ed5db,Using token-based signing to install unsigned binaries
下一篇:windows系统可以用android,如何在电脑上使用Android系统?

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月03日 09时24分05秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章