尝试从资产文件夹复制文件时发生错误 [英] An error when trying to copy a file from the assets folder

查看:105
本文介绍了尝试从资产文件夹复制文件时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件从资产文件夹复制到外部存储,这是我的代码:

I am trying to copy a file from assets folder to external storage, this is the code I have:

File f=new File(Environment.getExternalStorageDirectory(), "imgs/" + str2);
        if (!f.exists()) {
            try {
                InputStream ins = getApplicationContext().getAssets().open("imgs/" + str2);
                FileOutputStream out=new FileOutputStream(f);
                byte[] buf=new byte[1024];
                int len;
                while ((len=ins.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                ins.close();
                out.close();
            }
            catch (IOException e) {
                Log.e("FileProvider", "Exception copying from assets", e);
            }
        }

但是我得到的错误是: java.io.FileNotFoundException: /storage/emulated/0/imgs/pic_name.jpg (No such file or directory)

but the error I get is: java.io.FileNotFoundException: /storage/emulated/0/imgs/pic_name.jpg (No such file or directory)

但是,我正在创建此文件,所以我不知道为什么它说找不到该文件.

But, I am creating this file so I have no idea why it says that it's not found.

推荐答案

因为"img"目录不存在,必须创建目录

because "img" directory is not exists you must create the directory

代码:

   String rootPath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/imgs/";
        File file=new File(rootPath);
      if(!file.exists()){
         file.mkdirs();
       }

这篇关于尝试从资产文件夹复制文件时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆