尝试从资产文件夹复制文件时出错
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();
}
我正在尝试将文件从资产文件夹复制到外部存储,这是我的代码:
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();
}