add some checks to avoid displaying nonexistant files

This commit is contained in:
tibbi 2016-11-12 20:29:47 +01:00
parent f548ed833a
commit 34d015734e
3 changed files with 42 additions and 31 deletions

View file

@ -212,7 +212,8 @@ public class MediaActivity extends SimpleActivity
final Cursor cursor = getContentResolver().query(uri, columns, where, args, null); final Cursor cursor = getContentResolver().query(uri, columns, where, args, null);
final String pattern = Pattern.quote(mPath) + "/[^/]*"; final String pattern = Pattern.quote(mPath) + "/[^/]*";
if (cursor != null && cursor.moveToFirst()) { if (cursor != null) {
if (cursor.moveToFirst()) {
final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
do { do {
final String curPath = cursor.getString(pathIndex); final String curPath = cursor.getString(pathIndex);
@ -230,6 +231,7 @@ public class MediaActivity extends SimpleActivity
} }
} }
} while (cursor.moveToNext()); } while (cursor.moveToNext());
}
cursor.close(); cursor.close();
} }
} }

View file

@ -361,6 +361,7 @@ public class ViewPagerActivity extends SimpleActivity
private List<Medium> getMedia() { private List<Medium> getMedia() {
final List<Medium> media = new ArrayList<>(); final List<Medium> media = new ArrayList<>();
final ArrayList<File> invalidFiles = new ArrayList<>();
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
if (i == 1) { if (i == 1) {
@ -372,26 +373,35 @@ public class ViewPagerActivity extends SimpleActivity
final Cursor cursor = getContentResolver().query(uri, columns, where, args, null); final Cursor cursor = getContentResolver().query(uri, columns, where, args, null);
final String pattern = Pattern.quote(mDirectory) + "/[^/]*"; final String pattern = Pattern.quote(mDirectory) + "/[^/]*";
if (cursor != null && cursor.moveToFirst()) { if (cursor != null) {
if (cursor.moveToFirst()) {
final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
do { do {
final String curPath = cursor.getString(pathIndex); final String curPath = cursor.getString(pathIndex);
if (curPath == null) if (curPath == null)
continue; continue;
final File file = new File(curPath);
if (!file.exists()) {
invalidFiles.add(file);
continue;
}
if (curPath.matches(pattern) && !curPath.equals(mToBeDeleted) && !curPath.equals(mBeingDeleted)) { if (curPath.matches(pattern) && !curPath.equals(mToBeDeleted) && !curPath.equals(mBeingDeleted)) {
final int dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED); final int dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED);
final long timestamp = cursor.getLong(dateIndex); final long timestamp = cursor.getLong(dateIndex);
final int sizeIndex = cursor.getColumnIndex(MediaStore.Images.Media.SIZE); final int sizeIndex = cursor.getColumnIndex(MediaStore.Images.Media.SIZE);
final long size = cursor.getLong(sizeIndex); final long size = cursor.getLong(sizeIndex);
media.add(new Medium("", curPath, i == 1, timestamp, size)); media.add(new Medium(file.getName(), curPath, i == 1, timestamp, size));
} }
} while (cursor.moveToNext()); } while (cursor.moveToNext());
}
cursor.close(); cursor.close();
} }
} }
Utils.Companion.scanFiles(getApplicationContext(), invalidFiles);
Medium.Companion.setSorting(mConfig.getSorting()); Medium.Companion.setSorting(mConfig.getSorting());
Collections.sort(media); Collections.sort(media);
int j = 0; int j = 0;

View file

@ -44,7 +44,6 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
if (cursor != null) { if (cursor != null) {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
val pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA) val pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA)
do { do {
val fullPath: String = cursor.getString(pathIndex) ?: continue val fullPath: String = cursor.getString(pathIndex) ?: continue