问题
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