Merge "Broaden exception criteria for failed image URI resolution"
diff --git a/src/com/android/bluetooth/audio_util/helpers/Image.java b/src/com/android/bluetooth/audio_util/helpers/Image.java
index 1dd04c1..c0623c6 100644
--- a/src/com/android/bluetooth/audio_util/helpers/Image.java
+++ b/src/com/android/bluetooth/audio_util/helpers/Image.java
@@ -24,7 +24,6 @@
import android.os.Bundle;
import android.util.Log;
-import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
@@ -189,7 +188,8 @@
if (mContext == null) return null;
input = mContext.getContentResolver().openInputStream(uri);
art = BitmapFactory.decodeStream(input);
- } catch (FileNotFoundException e) {
+ } catch (Exception e) {
+ Log.w("Failed to fetch image at uri=" + uri, e);
art = null;
}
try {
diff --git a/tests/unit/src/com/android/bluetooth/audio_util/ImageTest.java b/tests/unit/src/com/android/bluetooth/audio_util/ImageTest.java
index 0188fa9..89bbf5c 100644
--- a/tests/unit/src/com/android/bluetooth/audio_util/ImageTest.java
+++ b/tests/unit/src/com/android/bluetooth/audio_util/ImageTest.java
@@ -67,6 +67,11 @@
.build();
private static final String IMAGE_STRING_1 = IMAGE_URI_1.toString();
+ private static final String IMAGE_HANDLE_SECURITY_ERROR = "sec_error";
+ private static final Uri IMAGE_URI_SECURITY_ERROR = TEST_CONTENT_URI.buildUpon()
+ .appendQueryParameter("handle", IMAGE_HANDLE_SECURITY_ERROR)
+ .build();
+
private Bitmap mTestBitmap = null;
@Before
@@ -91,6 +96,8 @@
if (IMAGE_URI_1.equals(url)) {
return mTestResources.openRawResourceFd(
com.android.bluetooth.tests.R.raw.image_200_200);
+ } else if (IMAGE_URI_SECURITY_ERROR.equals(url)) {
+ throw new SecurityException();
}
return null;
}
@@ -444,6 +451,18 @@
}
/**
+ * Make sure image URI resolution with erroneous resources doesn't crash and results in a null
+ * image.
+ */
+ @Test
+ public void testLoadImageFromUriWithSecurityException() {
+ Image artwork = new Image(mMockContext, IMAGE_URI_SECURITY_ERROR);
+ assertThat(artwork.getImageHandle()).isNull();
+ assertThat(artwork.getSource()).isEqualTo(Image.SOURCE_NONE);
+ assertThat(artwork.getImage()).isNull();
+ }
+
+ /**
* Make sure you can get a string representation of this Image
*/
@Test