File path = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "picture");
if (! path.exists()) {
path.mkdirs();
if (!path.exists()) {
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HH_mm_ss", Locale.CHINA).format(new Date());
File imagePath = new File(path.getPath() + "_" + "IMG_" + timeStamp + ".jpg");
BufferedOutputStream fos;
try {
fos =new BufferedOutputStream(new FileOutputStream(imagePath));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
return imagePath;
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
return null;
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
return null;
}
并且在清单中权限集是正确的
本期在Android派及更高版本。因此,在清单文件中添加这一行可以修复错误。
<application
...
...
android:requestLegacyExternalStorage="true">
</application>
以下代码包括从服务器下载文件并将其保存在存储中,当设备有内部存储时,该代码可以正常工作。 但当我尝试使用没有内部存储的设备时,只使用外部存储时,我得到以下异常。 有什么建议吗?原因是什么?顺便提一下,Environment.getExternalStorageDirectory()返回/mnt/sdcard/,file.mkdir()返回false。
更新 结果发现我调用open和close FileOutputStream的频率太高了,这会在某个时候抛出FileNotFoundException。听起来更像是线程问题。
问题内容: 我一直在尝试加密文件,并将这些文件写回到同一位置。但是我收到错误消息说 我的档案是这个 我认为我已经提供了正确的许可。我用来加密文件的代码就是这个。 我在按钮内使用了这种方法 仍然我无法配置此错误。请有人帮忙! 问题答案: 我怀疑您运行的是Android 6.0棉花糖(API 23)或更高版本。在这种情况下, 必须先 实现运行时权限,然后才能尝试读取/写入外部存储。
java.io.FileNotFoundException:/Storage/Emulated/0/WhatsApp/Media/WhatsApp Documents/api.txt:open失败:EACCES(拒绝权限)
在其他设备中没有发生这种情况,我测试的设备:、、