无法将图像缓存到文件,说文件不存在
问题描述:
我希望我的代码中有一个明显的问题,我忽略了,但是我正在验证将要写入的文件存在并可以写入。它在fos = new FileOutputStream(file.getPath());
上崩溃,FileNotFound
异常。无法将图像缓存到文件,说文件不存在
File file = new File(cacheDir, url.getFile());
if (file.exists()) {
file.delete();
}
try {
file.mkdirs();
file.createNewFile();
}
catch (IOException e) {
MyLog.e("Unable to create file: " + file.getPath());
}
if (file.exists() && file.canWrite()) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
if (!bitmap.compress(CompressFormat.PNG, 100, fos)) {
MyLog.e("Unable to cache image: " + path);
}
if (fos != null) {
try {
fos.flush();
fos.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
答
我敢打赌,你在文件名中有一些有趣的人物。
尝试
File file = new File(cacheDir, URLEncoder.encode(url.getFile()));