【问题标题】:How clear gallery thumbnails after deleting image from DCIM in Android在 Android 中从 DCIM 中删除图像后如何清除画廊缩略图
【发布时间】:2016-05-18 06:35:12
【问题描述】:

我在捕获后删除图像并对其进行一些工作。但是在删除图像库后直到保留缩略图。从 Android 中的 DCIM 删除图像后,画廊缩略图如何清晰。

private boolean deleteLastFromDCIM() {

boolean success = false;
try {
    File[] images = new File(Environment.getExternalStorageDirectory() + File.separator + "DCIM"+File.separator+"Camera").listFiles();
    File latestSavedImage = images[0];
    for (int i = 1; i < images.length; ++i) {
        if (images[i].lastModified() > latestSavedImage.lastModified()) {
            latestSavedImage = images[i];
        }
    }

            // OR JUST Use  success = latestSavedImage.delete();
    success = new File(Environment.getExternalStorageDirectory()
            + File.separator + "DCIM/Camera/"
            + latestSavedImage.getAbsoluteFile()).delete();
    return success;
} catch (Exception e) {
    e.printStackTrace();
    return success;
}}


if(file.exists()){
    Calendar time = Calendar.getInstance();
    time.add(Calendar.DAY_OF_YEAR,-7);
    //I store the required attributes here and delete them
    Date lastModified = new Date(file.lastModified());
    if(lastModified.before(time.getTime()))
    {
        //file is older than a week
    }
    file.delete();
}else{
    file.createNewFile();
}

【问题讨论】:

标签: java android


【解决方案1】:

只需要写一个删除文件的方法并替换代码 file.delete();与 deleteFileFromMediaStore(this.getContentResolver(),file);

    public  void deleteFileFromMediaStore(final ContentResolver contentResolver, final File file) {
    String canonicalPath;
    try {
        canonicalPath = file.getCanonicalPath();
    } catch (IOException e) {
        canonicalPath = file.getAbsolutePath();
    }
    final Uri uri = MediaStore.Files.getContentUri("external");
    final int result = contentResolver.delete(uri,
            MediaStore.Files.FileColumns.DATA + "=?", new String[] {canonicalPath});
    if (result == 0) {
        final String absolutePath = file.getAbsolutePath();
        if (!absolutePath.equals(canonicalPath)) {
            contentResolver.delete(uri,
                    MediaStore.Files.FileColumns.DATA + "=?", new String[]{absolutePath});
        }
    }
}

【讨论】:

  • 感谢你@Diganta
  • 适用于安卓 5.0。但它不适用于 android 6.0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-21
  • 1970-01-01
  • 2021-07-06
  • 2018-04-25
  • 2012-01-31
  • 1970-01-01
相关资源
最近更新 更多