Android 12 删除图像文件不起作用
对于 android 12,我无法删除应用程序创建的图像文件。方法 File.delete() 被忽略,我没有收到错误,但文件在它之后仍然存在。
图像文件是通过按“添加照片按钮”创建的,第一次按下后将调用此方法:
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
File photoFile = createImageFile();
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}catch (Exception e){
Log.i("ERROR",e.getMessage());
}
}
创建图像文件方法:
private File createImageFile() {
String imageFileName = String.format(new Locale("pl"), PHOTO_FILE_FORMAT, currentMonth.getMonth(), currentMonth.getYear());
File storageDir = new File(ShiftCalendarApplication.PHOTO_DIRECTORY);
if (!storageDir.exists()) {
if(!storageDir.mkdirs())
return null;
}
return new File(ShiftCalendarApplication.PHOTO_DIRECTORY, imageFileName + FILE_EXTENSION);
}
照片目录:
public static final String PHOTO_DIRECTORY = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath() + "/My App/" + "Zdjecia/";
按另一个调用此方法的按钮删除文件,当用户在询问时选择“是”删除。
private void deleteSelectedImage() {
if (photos.get(monthsFromToday) != null) {
AlertDialog.Builder builder = new AlertDialog.Builder(PhotoOfScheduleActivity.this);
builder.setTitle("Usuń zdjęcie")
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage("Czy na pewno usunąć?")
.setPositiveButton("Usuń", (dialog, which) -> {
File file = new File(paths.get(monthsFromToday));
Log.d("SAVED_IMAGE", "(BEFORE)FILE EXIST: " + file.exists());
file.delete();
Log.d("SAVED_IMAGE", "(AFTER)FILE EXIST: " + file.exists()); // FILE STILL EXIST HERE
reloadDrawables();
adapter = new SchedulePhotosPagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(monthsFromToday);
})
.setCancelable(true)
.setNegativeButton("Anuluj", null)
.show();
}
}
重新加载存储照片路径的 Drawabled 方法:
private void reloadDrawables() {
File directory = new File(ShiftCalendarApplication.PHOTO_DIRECTORY);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
photos = new SparseArray<>();
paths = new SparseArray<>();
int year = currentMonth.getYear();
int month = currentMonth.getMonth();
File[] files = directory.listFiles();
if (directory.listFiles() != null) {
for (File file : files) {
if (file.isFile() && file.getName().endsWith(FILE_EXTENSION)) {
try {
Photo photo = new Photo(removeExtension(file.getName()), file.getPath());
String[] date = photo.getPhotoName().split("-");
int pYear = Integer.parseInt(date[1]);
int pMonth = Integer.parseInt(date[0]);
int key = monthsFromToday;
key += (pYear - year) * 12;
key += pMonth - month;
paths.put(key, file.getPath());
photos.put(key, photo);
} catch (Throwable e) {
Log.e(TAG, e.getMessage());
}
}
}
}
if (adapter != null) {
adapter.notifyDataSetChanged();
}
}
File.delete() 不适用于 android >在本例中为 29
For android 12 i cannot remove image file created by app. Method File.delete() is ignored, i don't get error but file still exist after it.
Image file is created by press "Add photo button", after pressing at first this method is invoked:
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
File photoFile = createImageFile();
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}catch (Exception e){
Log.i("ERROR",e.getMessage());
}
}
Create image file method:
private File createImageFile() {
String imageFileName = String.format(new Locale("pl"), PHOTO_FILE_FORMAT, currentMonth.getMonth(), currentMonth.getYear());
File storageDir = new File(ShiftCalendarApplication.PHOTO_DIRECTORY);
if (!storageDir.exists()) {
if(!storageDir.mkdirs())
return null;
}
return new File(ShiftCalendarApplication.PHOTO_DIRECTORY, imageFileName + FILE_EXTENSION);
}
Photo directory:
public static final String PHOTO_DIRECTORY = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath() + "/My App/" + "Zdjecia/";
File is delete by pressing another button which invoke this method is user select "yes" when is asked about deletion.
private void deleteSelectedImage() {
if (photos.get(monthsFromToday) != null) {
AlertDialog.Builder builder = new AlertDialog.Builder(PhotoOfScheduleActivity.this);
builder.setTitle("Usuń zdjęcie")
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage("Czy na pewno usunąć?")
.setPositiveButton("Usuń", (dialog, which) -> {
File file = new File(paths.get(monthsFromToday));
Log.d("SAVED_IMAGE", "(BEFORE)FILE EXIST: " + file.exists());
file.delete();
Log.d("SAVED_IMAGE", "(AFTER)FILE EXIST: " + file.exists()); // FILE STILL EXIST HERE
reloadDrawables();
adapter = new SchedulePhotosPagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(monthsFromToday);
})
.setCancelable(true)
.setNegativeButton("Anuluj", null)
.show();
}
}
Reload Drawabled method which store paths for photos:
private void reloadDrawables() {
File directory = new File(ShiftCalendarApplication.PHOTO_DIRECTORY);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
photos = new SparseArray<>();
paths = new SparseArray<>();
int year = currentMonth.getYear();
int month = currentMonth.getMonth();
File[] files = directory.listFiles();
if (directory.listFiles() != null) {
for (File file : files) {
if (file.isFile() && file.getName().endsWith(FILE_EXTENSION)) {
try {
Photo photo = new Photo(removeExtension(file.getName()), file.getPath());
String[] date = photo.getPhotoName().split("-");
int pYear = Integer.parseInt(date[1]);
int pMonth = Integer.parseInt(date[0]);
int key = monthsFromToday;
key += (pYear - year) * 12;
key += pMonth - month;
paths.put(key, file.getPath());
photos.put(key, photo);
} catch (Throwable e) {
Log.e(TAG, e.getMessage());
}
}
}
}
if (adapter != null) {
adapter.notifyDataSetChanged();
}
}
File.delete() not working for for android > 29 in this case
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
着眼于 API 29,有很多 非-SDK接口已停止工作。 java.io.file 是受影响的文件之一,因此它不再起作用。
这就是我解决问题的方法:
Staring on API 29, there a are a lot of non-SDK interfaces that have stopped working. java.io.file is one of the affected ones so it's no longer functional.
This is how I solved the issue:
从 Android 11 及更高版本开始
从外部或内部存储中删除文件。
公共或私人路径。
使用MediaStore删除文件。
这个答案用于删除任何媒体文件。(稍作修改)
https://stackoverflow.com/a/75640130/14035342
您可以使用相同的代码答案 。
只需将答案链接中的此方法更改为这样
from Android 11 an higher
to delete file from external or internal Storage.
public or private path.
use MediaStore to Delete Files.
this Answer for delete any Media File.(With a slight modification)
https://stackoverflow.com/a/75640130/14035342
you can use same code Answer.
only Change this method in the answer link to like this