繁体   English   中英

尝试从资产文件夹复制文件时发生错误

[英]An error when trying to copy a file from the assets folder

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

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)

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

因为“ img”目录不存在,所以必须创建目录

码:

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

暂无
暂无

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

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