couple more checks to prevent displaying invalid images

This commit is contained in:
tibbi 2016-06-03 18:43:54 +02:00
parent c8e77e5362
commit 4813a2d5ee
2 changed files with 13 additions and 1 deletions

View File

@ -132,6 +132,7 @@ public class MainActivity extends AppCompatActivity
final String[] columns = {MediaStore.Images.Media.DATA}; final String[] columns = {MediaStore.Images.Media.DATA};
final String order = MediaStore.Images.Media.DATE_MODIFIED + " DESC"; final String order = MediaStore.Images.Media.DATE_MODIFIED + " DESC";
final Cursor cursor = getContentResolver().query(uri, columns, null, null, order); final Cursor cursor = getContentResolver().query(uri, columns, null, null, order);
final List<String> invalidFiles = new ArrayList<>();
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); 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 File file = new File(path);
final String fileDir = file.getParent(); final String fileDir = file.getParent();
if (!file.exists()) {
invalidFiles.add(file.getAbsolutePath());
continue;
}
if (directories.containsKey(fileDir)) { if (directories.containsKey(fileDir)) {
final Directory directory = directories.get(fileDir); final Directory directory = directories.get(fileDir);
final int newImageCnt = directory.getPhotoCnt() + 1; final int newImageCnt = directory.getPhotoCnt() + 1;
@ -152,6 +158,9 @@ public class MainActivity extends AppCompatActivity
cursor.close(); cursor.close();
} }
final String[] invalids = invalidFiles.toArray(new String[invalidFiles.size()]);
MediaScannerConnection.scanFile(getApplicationContext(), invalids, null, null);
return directories; return directories;
} }

View File

@ -138,7 +138,10 @@ public class PhotosActivity extends AppCompatActivity
do { do {
final String curPath = cursor.getString(pathIndex); final String curPath = cursor.getString(pathIndex);
if (curPath.matches(pattern) && !toBeDeleted.contains(curPath)) { 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()); } while (cursor.moveToNext());
cursor.close(); cursor.close();