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

守給你的承諾、 提交于 2020-01-06 07:16:10

问题


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

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.


回答1:


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

code:

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


来源:https://stackoverflow.com/questions/53996159/an-error-when-trying-to-copy-a-file-from-the-assets-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!