From 34d015734efb9b210a08484e33f85cc0dc9aeaae Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 12 Nov 2016 20:29:47 +0100 Subject: [PATCH] add some checks to avoid displaying nonexistant files --- .../gallery/activities/MediaActivity.java | 34 +++++++++-------- .../gallery/activities/ViewPagerActivity.java | 38 ++++++++++++------- .../asynctasks/GetDirectoriesAsynctask.kt | 1 - 3 files changed, 42 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java index 27590d129..245025e7f 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java @@ -212,24 +212,26 @@ public class MediaActivity extends SimpleActivity final Cursor cursor = getContentResolver().query(uri, columns, where, args, null); final String pattern = Pattern.quote(mPath) + "/[^/]*"; - if (cursor != null && cursor.moveToFirst()) { - final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); - do { - final String curPath = cursor.getString(pathIndex); - if (curPath == null) - continue; + if (cursor != null) { + if (cursor.moveToFirst()) { + final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); + do { + final String curPath = cursor.getString(pathIndex); + if (curPath == null) + continue; - if (curPath.matches(pattern) && !mToBeDeleted.contains(curPath)) { - final File file = new File(curPath); - if (file.exists()) { - final int dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED); - final long timestamp = cursor.getLong(dateIndex); - media.add(new Medium(file.getName(), curPath, (i == 1), timestamp, file.length())); - } else { - invalidFiles.add(file); + if (curPath.matches(pattern) && !mToBeDeleted.contains(curPath)) { + final File file = new File(curPath); + if (file.exists()) { + final int dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED); + final long timestamp = cursor.getLong(dateIndex); + media.add(new Medium(file.getName(), curPath, (i == 1), timestamp, file.length())); + } else { + invalidFiles.add(file); + } } - } - } while (cursor.moveToNext()); + } while (cursor.moveToNext()); + } cursor.close(); } } diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java index 5be8ca145..d4953719d 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java @@ -361,6 +361,7 @@ public class ViewPagerActivity extends SimpleActivity private List getMedia() { final List media = new ArrayList<>(); + final ArrayList invalidFiles = new ArrayList<>(); for (int i = 0; i < 2; i++) { Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; if (i == 1) { @@ -372,26 +373,35 @@ public class ViewPagerActivity extends SimpleActivity final Cursor cursor = getContentResolver().query(uri, columns, where, args, null); final String pattern = Pattern.quote(mDirectory) + "/[^/]*"; - if (cursor != null && cursor.moveToFirst()) { - final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); - do { - final String curPath = cursor.getString(pathIndex); - if (curPath == null) - continue; + if (cursor != null) { + if (cursor.moveToFirst()) { + final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); + do { + final String curPath = cursor.getString(pathIndex); + if (curPath == null) + continue; - if (curPath.matches(pattern) && !curPath.equals(mToBeDeleted) && !curPath.equals(mBeingDeleted)) { - final int dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED); - final long timestamp = cursor.getLong(dateIndex); + final File file = new File(curPath); + if (!file.exists()) { + invalidFiles.add(file); + continue; + } - final int sizeIndex = cursor.getColumnIndex(MediaStore.Images.Media.SIZE); - final long size = cursor.getLong(sizeIndex); - media.add(new Medium("", curPath, i == 1, timestamp, size)); - } - } while (cursor.moveToNext()); + if (curPath.matches(pattern) && !curPath.equals(mToBeDeleted) && !curPath.equals(mBeingDeleted)) { + final int dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED); + final long timestamp = cursor.getLong(dateIndex); + + final int sizeIndex = cursor.getColumnIndex(MediaStore.Images.Media.SIZE); + final long size = cursor.getLong(sizeIndex); + media.add(new Medium(file.getName(), curPath, i == 1, timestamp, size)); + } + } while (cursor.moveToNext()); + } cursor.close(); } } + Utils.Companion.scanFiles(getApplicationContext(), invalidFiles); Medium.Companion.setSorting(mConfig.getSorting()); Collections.sort(media); int j = 0; diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt index 054489353..1578af556 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt @@ -44,7 +44,6 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va if (cursor != null) { if (cursor.moveToFirst()) { - val pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA) do { val fullPath: String = cursor.getString(pathIndex) ?: continue