Qihoo360/RePlugin

最新版本2.2.0 插件中使用Glide加载本地图片报错

Closed this issue · 6 comments

问题详细描述 Detailed description of the problem

在2.1.7版本中使用Glide加载本地图片正常,然后升级为2.2.0版本后插件中加载图片出现了问题。

复现问题步骤 Steps to reproduce the problem

  1. 使用版本2.2.0最新版本,
    Glide使用 compile 'com.github.bumptech.glide:glide:4.0.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.0.0'
  2. 加载本地图片代码:
    Glide.with(Main2Activity.this)
    .load(R.drawable.banner_ryx)
    .into(imageView);
    错误日志:
    java.io.FileNotFoundException: No package found for authority: android.resource://com.qihoo360.replugin.sample.demo2/drawable/banner_ryx
    at android.content.ContentResolver.getResourceId(ContentResolver.java:1237)
    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:979)
    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:914)
    at com.bumptech.glide.load.data.FileDescriptorLocalUriFetcher.loadResource(FileDescriptorLocalUriFetcher.java:21)
    at com.bumptech.glide.load.data.FileDescriptorLocalUriFetcher.loadResource(FileDescriptorLocalUriFetcher.java:13)
    at com.bumptech.glide.load.data.LocalUriFetcher.loadData(LocalUriFetcher.java:40)
    at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:95)
    at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:61)
    at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:282)
    at com.bumptech.glide.load.engine.DecodeJob.onDataFetcherFailed(DecodeJob.java:377)
    at com.bumptech.glide.load.engine.SourceGenerator.onLoadFailed(SourceGenerator.java:118)
    at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.startNextOrFail(MultiModelLoader.java:146)
    at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.onLoadFailed(MultiModelLoader.java:138)
    at com.bumptech.glide.load.data.LocalUriFetcher.loadData(LocalUriFetcher.java:45)
    at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:95)
    at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:61)
    at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:282)
    at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:252)
    at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:222)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    at java.lang.Thread.run(Thread.java:818)
    at com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:347)

其它重要信息 Other important information

replugin-host-lib/gradle Version:2.20
rePlugin-plugin-lib/gradle Version:2.20

Android API Version:25.0.0
Android 手机型号&ROM(Phone model & ROM):
Samsung SM-C7010 Android 6.0.1 Api23

不得不吐槽下360的工程师们,GitHub上提交了issues无人应,群里单独问了群主,答应的说是给看下但是一直没有任何下文,最后只能自己来关闭这个issues了。一个网友告诉Glide已经有了4.1.1版本,将Glide的升级到4.4.1版本后可以正常加载本地图片。具体原因暂不明。

这个已经在看了,但定位问题没有你想象中的那么简单。何况我们还在处理一些新的Issue,不可能每个人提出的问题都是最高优先级(虽然你自己可能会这么认为)。这个我在群里都解释了。希望大家都能理解,共同让RePlugin变得更好。如果有兴趣,欢迎随时提PR。

你好这个问题解决了吗?

不得不吐槽下360的工程师们,GitHub上提交了issues无人应,群里单独问了群主,答应的说是给看下但是一直没有任何下文,最后只能自己来关闭这个issues了。一个网友告诉Glide已经有了4.1.1版本,将Glide的升级到4.4.1版本后可以正常加载本地图片。具体原因暂不明。

glide升到了4.8.0好像还是有这个问题。

不得不吐槽下360的工程师们,GitHub上提交了issues无人应,群里单独问了群主,答应的说是给看下但是一直没有任何下文,最后只能自己来关闭这个issues了。一个网友告诉Glide已经有了4.1.1版本,将Glide的升级到4.4.1版本后可以正常加载本地图片。具体原因暂不明。

已经花了三年时间来解决

插件化应该都有这个问题
####下面是我的修复方案####

  1. 自定义ResourceDrawableDecoder,修改点:
@Nullable
    @Override
    public Resource<Drawable> decode(@NonNull Uri source, int width, int height,
                                     @NonNull Options options) {
        @DrawableRes int resId = loadResourceIdFromUri(source);
        MLog.info(TAG, "source:%s", source);
        //加载插件的drawable时,packageName是插件applicationID,非host applicationID
//        String packageName = source.getAuthority();
//        Context targetContext = packageName.equals(context.getPackageName())
//                ? context : getContextForPackage(source, packageName);
//         We can't get a theme from another application.
        Drawable drawable = DrawableDecoderCompat.getDrawable(context, resId, null);
        return XXNonOwnedDrawableResource.newInstance(drawable);
    }
  1. 自定义ResourceBitmapDecoder
@Nullable
    @Override
    public Resource<Bitmap> decode(@NonNull Uri source, int width, int height,
                                   @NonNull Options options) {
        //drawableDecoder是第一步的decoder
        Resource<Drawable> drawableResource = drawableDecoder.decode(source, width, height, options);
        if (drawableResource == null) {
            return null;
        }
        Drawable drawable = drawableResource.get();
        return XXDrawableToBitmapConverter.convert(bitmapPool, drawable, width, height);
    }
  1. AppGlideModule注册registerComponents,替换glide解码器
  /* Drawables */
                .prepend(Uri.class, Drawable.class, resourceDrawableDecoder)
                .prepend(
                        Uri.class, Bitmap.class, new XXResourceBitmapDecoder(resourceDrawableDecoder, bitmapPool));