diff --git a/library/src/main/java/com/bumptech/glide/load/resource/drawable/ResourceDrawableDecoder.java b/library/src/main/java/com/bumptech/glide/load/resource/drawable/ResourceDrawableDecoder.java index f37045c7fc..9b8f5a2852 100644 --- a/library/src/main/java/com/bumptech/glide/load/resource/drawable/ResourceDrawableDecoder.java +++ b/library/src/main/java/com/bumptech/glide/load/resource/drawable/ResourceDrawableDecoder.java @@ -47,18 +47,27 @@ public Resource decode(@NonNull Uri source, int width, int height, @NonNull Options options) { @DrawableRes int resId = loadResourceIdFromUri(source); String packageName = source.getAuthority(); - Context targetContext = packageName.equals(context.getPackageName()) - ? context : getContextForPackage(source, packageName); + Context targetContext = findContextForPackage(source, packageName); // We can't get a theme from another application. Drawable drawable = DrawableDecoderCompat.getDrawable(context, targetContext, resId); return NonOwnedDrawableResource.newInstance(drawable); } @NonNull - private Context getContextForPackage(Uri source, String packageName) { + private Context findContextForPackage(Uri source, String packageName) { + // Fast path + if (packageName.equals(context.getPackageName())) { + return context; + } + try { return context.createPackageContext(packageName, /*flags=*/ 0); } catch (NameNotFoundException e) { + // The parent APK holds the correct context if the resource is located in a split + if (packageName.contains(context.getPackageName())) { + return context; + } + throw new IllegalArgumentException( "Failed to obtain context or unrecognized Uri format for: " + source, e); }