我在这里发现了类似的问题,但发现解决方案对我不起作用。 我已经从网上下载了一张图片并将其保存到了文件目录
中private boolean saveImageToDisk(ResponseBody body, Context context, String fileName) {
try {
File image = new File(context.getFilesDir() + File.separator + fileName);
InputStream inputStream = null;
OutputStream outputStream = null;
try {
byte[] fileReader = new byte[4096];
inputStream = body.byteStream();
outputStream = new FileOutputStream(image);
while (true) {
int read = inputStream.read(fileReader);
if (read == -1) {
break;
}
outputStream.write(fileReader, 0, read);
}
outputStream.flush();
return true;
} catch (IOException e) {
return false;
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
} catch (IOException e) {
return false;
}
}
它运行正常,但当我尝试获取文件并转换为Bitmap
时,我总是得到null
,尽管file.exists()
返回true
private Bitmap getImageFromCache(Context context, String fileName) {
String fname = new File(context.getFilesDir(), fileName).getAbsolutePath();
Bitmap bitmap = BitmapFactory.decodeFile(fname);
return bitmap; // bitmap = null
}
我的AndroidManifest中有权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />