android - File.delete() 函数在 android 4.2.2 版本中不起作用
问题描述
我正在尝试使用以下代码删除相机捕获的图像,但图像没有删除我已经尝试了很多但仍然没有结果有人可以帮助我吗
代码:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, Constants.CAMERA_REQUEST_CODE);
private void onCaptureImageResult(Intent data) {
File file = saveImage(this, data);
if(file !=null){
file .getCanonicalFile().delete();
}
}
public File saveImage(Context context, Intent data) {
File mediaFile = null;
try {
Bitmap imgBitmap = (Bitmap) data.getExtras().get("data");
File sd = Environment.getExternalStorageDirectory();
File imageFolder = new File(sd.getAbsolutePath() + File.separator +
"FOSImages");
if (!imageFolder.isDirectory()) {
imageFolder.mkdirs();
}
mediaFile = new File(imageFolder + File.separator + "fos_" +
System.currentTimeMillis() + ".jpg");
FileOutputStream fileOutputStream = new FileOutputStream(mediaFile);
imgBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fileOutputStream);
fileOutputStream.close();
return mediaFile;
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return mediaFile;
}
解决方案
尝试这个 :
File file = saveImage(this, data);
file.delete();
if(file.exists()){
file.getCanonicalFile().delete();
if(file.exists()){
getApplicationContext().deleteFile(file.getName());
}
}
推荐阅读
- java - Android 10:返回 Activity 后 exitTransition 重置
- javascript - 为什么我的网页显示 [object HTMLParagraphElement]?
- logging - LoggerFactory 未生成日志
- python - 如何使用 spaCy 的基于规则的匹配进行句子提取
- php - 正则表达式中使用的字符“~”是什么?
- python - 如何计算字符串python中某些实体的“自然”外观
- c# - NodeRSA 不会生成正确的加密字符串以供 RSACryptoServiceProvider C# 解密
- vb.net - 如何阻止 MDIchild 表单在不同位置打开
- bash - 如何使用awk根据前两个字段找出第三个字段的最小值
- c - C 中的链表实现失败