Skip to content
Permalink
Browse files

Catch all Exceptions from external query() calls

query() calls can fail with many kinds of RuntimeExceptions. We should be resilient to all of them.

Fatal Exception: java.lang.IllegalStateException: java.io.FileNotFoundException: No item at content://media/external/downloads/333480
       at android.os.Parcel.createException(Parcel.java:2079)
       at android.os.Parcel.readException(Parcel.java:2039)
       at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
       at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
       at android.content.ContentProviderProxy.query(ContentProviderProxy.java:423)
       at android.content.ContentResolver.query(ContentResolver.java:944)
       at android.content.ContentResolver.query(ContentResolver.java:880)
       at android.content.ContentResolver.query(ContentResolver.java:836)
       at com.google.android.apps.muzei.gallery.GallerySettingsActivity.getImagesFromTreeUri(GallerySettingsActivity.java:718)
       at com.google.android.apps.muzei.gallery.GallerySettingsActivity.access$getImagesFromTreeUri(GallerySettingsActivity.java:82)
       at com.google.android.apps.muzei.gallery.GallerySettingsActivity$GalleryAdapter.onBindViewHolder(GallerySettingsActivity.java:632)
       at com.google.android.apps.muzei.gallery.GallerySettingsActivity$GalleryAdapter.onBindViewHolder(GallerySettingsActivity.java:601)
       at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7046)
  • Loading branch information...
ianhanniballake committed Oct 9, 2019
1 parent 0752139 commit 38a34b1b4b4fc3d6dcc82cb7475f1bd567bdf91b
@@ -46,6 +46,7 @@ import com.google.android.apps.muzei.gallery.BuildConfig.GALLERY_ART_AUTHORITY
import com.google.android.apps.muzei.util.getString
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.FileNotFoundException
import java.io.IOException
import java.text.ParseException
import java.text.SimpleDateFormat
@@ -157,6 +158,9 @@ class GalleryScanWorker(
} catch (e: SecurityException) {
Log.w(TAG, "Unable to access image from $imageUri, deleting row", e)
deleteChosenPhoto(chosenPhoto)
} catch (e: Exception) {
// Could be anything: NullPointerException, IllegalArgumentException, etc.
Log.i(TAG, "Unable to load images from $imageUri", e)
}
}

@@ -201,6 +205,12 @@ class GalleryScanWorker(
} catch (e: SecurityException) {
Log.w(TAG, "Unable to load images from $treeUri, deleting row", e)
deleteChosenPhoto(chosenPhoto)
} catch (e: FileNotFoundException) {
Log.w(TAG, "Unable to load images from $treeUri, deleting row", e)
deleteChosenPhoto(chosenPhoto)
} catch (e: Exception) {
// Could be anything: NullPointerException, IllegalArgumentException, etc.
Log.i(TAG, "Unable to load images from $treeUri", e)
}
}

@@ -245,6 +255,7 @@ class GalleryScanWorker(
// No longer can read this URI, which means no children from this URI
} catch (e: Exception) {
// Could be anything: NullPointerException, IllegalArgumentException, etc.
Log.i(TAG, "Unable to load images from $treeUri", e)
}
}
}
@@ -31,6 +31,7 @@ import android.os.Bundle
import android.os.Handler
import android.provider.DocumentsContract
import android.provider.Settings
import android.util.Log
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
@@ -73,6 +74,7 @@ import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.lang.Exception
import java.util.ArrayList
import java.util.HashSet
import java.util.LinkedList
@@ -83,6 +85,7 @@ class GallerySettingsActivity : AppCompatActivity(), Observer<PagedList<ChosenPh
GalleryImportPhotosDialogFragment.OnRequestContentListener, MultiSelectionController.Callbacks {

companion object {
private const val TAG = "GallerySettingsActivity"
private const val SHARED_PREF_NAME = "GallerySettingsActivity"
private const val SHOW_INTERNAL_STORAGE_MESSAGE = "show_internal_storage_message"
private const val REQUEST_CHOOSE_PHOTOS = 1
@@ -735,7 +738,9 @@ class GallerySettingsActivity : AppCompatActivity(), Observer<PagedList<ChosenPh
}
} catch (e: SecurityException) {
// No longer can read this URI, which means no children from this URI
} catch (e: NullPointerException) {
} catch (e: Exception) {
// Could be anything: NullPointerException, IllegalArgumentException, etc.
Log.i(TAG, "Unable to load images from $treeUri", e)
}
}
return images

0 comments on commit 38a34b1

Please sign in to comment.
You can’t perform that action at this time.