Skip to content

Commit

Permalink
Catch/ignore SecurityException opening MediaStore thumbnails.
Browse files Browse the repository at this point in the history
A Uri permission grant for a MediaStore Uri will only grant access to the main Uri, not the thumbnail Uri. Applications that have only Uri permissions and not the storage runtime permissions will receive a SecurityException when they try to open the corresponding thumbnail. 

Prior to this change, the thumbnail exception would cause the entire
request to fail. After this change only the thumbnail portion of the request will fail so the image may still be loaded via the original MediaStore Uri. 

Fixes #3504.
  • Loading branch information
sjudd committed Mar 5, 2019
1 parent d77c0cb commit 9c48b48
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,19 @@ public InputStream open(Uri uri) throws FileNotFoundException {

@Nullable
private String getPath(@NonNull Uri uri) {
final Cursor cursor = query.query(uri);
Cursor cursor = null;
try {
cursor = query.query(uri);
if (cursor != null && cursor.moveToFirst()) {
return cursor.getString(0);
} else {
return null;
}
} catch (SecurityException e) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Failed to query for thumbnail for Uri: " + uri, e);
}
return null;
} finally {
if (cursor != null) {
cursor.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.bumptech.glide.load.data.mediastore;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -89,6 +91,12 @@ public void testReturnsOpenedInputStreamWhenFileFound() throws FileNotFoundExcep
assertEquals(expected, harness.get().open(harness.uri));
}

@Test
public void open_returnsNull_whenQueryThrowsSecurityException() throws FileNotFoundException {
when(harness.query.query(any(Uri.class))).thenThrow(new SecurityException());
assertThat(harness.get().open(harness.uri)).isNull();
}

@Test
public void testVideoQueryReturnsVideoCursor() {
Uri queryUri = MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI;
Expand Down Expand Up @@ -123,7 +131,7 @@ private static class Harness {
final FileService service = mock(FileService.class);
final ArrayPool byteArrayPool = new LruArrayPool();

public Harness() {
Harness() {
cursor.addRow(new String[] { file.getAbsolutePath() });
when(query.query(eq(uri))).thenReturn(cursor);
when(service.get(eq(file.getAbsolutePath()))).thenReturn(file);
Expand Down

0 comments on commit 9c48b48

Please sign in to comment.