From 4813a2d5ee5b31853c193a10ab873e606472fdb6 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 3 Jun 2016 18:43:54 +0200 Subject: [PATCH] couple more checks to prevent displaying invalid images --- .../gallery/activities/MainActivity.java | 9 +++++++++ .../gallery/activities/PhotosActivity.java | 5 ++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java index 64ad0f210..073981085 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java @@ -132,6 +132,7 @@ public class MainActivity extends AppCompatActivity final String[] columns = {MediaStore.Images.Media.DATA}; final String order = MediaStore.Images.Media.DATE_MODIFIED + " DESC"; final Cursor cursor = getContentResolver().query(uri, columns, null, null, order); + final List invalidFiles = new ArrayList<>(); if (cursor != null && cursor.moveToFirst()) { final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); @@ -140,6 +141,11 @@ public class MainActivity extends AppCompatActivity final File file = new File(path); final String fileDir = file.getParent(); + if (!file.exists()) { + invalidFiles.add(file.getAbsolutePath()); + continue; + } + if (directories.containsKey(fileDir)) { final Directory directory = directories.get(fileDir); final int newImageCnt = directory.getPhotoCnt() + 1; @@ -152,6 +158,9 @@ public class MainActivity extends AppCompatActivity cursor.close(); } + final String[] invalids = invalidFiles.toArray(new String[invalidFiles.size()]); + MediaScannerConnection.scanFile(getApplicationContext(), invalids, null, null); + return directories; } diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/PhotosActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/PhotosActivity.java index baf0aa952..86ff2b8df 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/PhotosActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/PhotosActivity.java @@ -138,7 +138,10 @@ public class PhotosActivity extends AppCompatActivity do { final String curPath = cursor.getString(pathIndex); if (curPath.matches(pattern) && !toBeDeleted.contains(curPath)) { - myPhotos.add(cursor.getString(pathIndex)); + final File file = new File(curPath); + if (file.exists()) { + myPhotos.add(cursor.getString(pathIndex)); + } } } while (cursor.moveToNext()); cursor.close();