Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

插件化后Glide无法获取本地资源文件 #593

Open
1 task done
xypeng12345 opened this issue Sep 28, 2018 · 3 comments
Open
1 task done

插件化后Glide无法获取本地资源文件 #593

xypeng12345 opened this issue Sep 28, 2018 · 3 comments

Comments

@xypeng12345
Copy link

  • 我已阅读并理解 贡献指南,严格遵循其约定。

错误报告

在插件化APP中使用Glide获取本地资源文件,报错无法找到。

No package found for authority: android.resource://cn.hotupdate.app.main/drawable/guide03
android.content.pm.PackageManager$NameNotFoundException: Application package cn.hotUpdate.app.main not found
主项目是cn.hotUpdate.app。,插件是.main

Glide能够正确的获取本地资源图片,或是有规避此情况的方法。

实际结果是什么?

Glide 无法获取本地资源

Small环境

Compile-time

  gradle-small plugin : 1.5.0-beta2 (maven)
            small aar : 1.5.0-beta2 (maven)
          gradle core : 4.4
       android plugin : 2.3.3
                   OS : Windows 10 10.0 (amd64)

Bundles

type name PP sdk aapt support file size
host app 27 26.0.2 27.1.1
stub app+stub 27 26.0.2 27.1.1
app app.main 0x77 27 26.0.2 27.1.1 *.main.apk 7 MB

Runtime

  Device : Samsung Nexus S
     SDK : Android 7.0
     ABI : armeabi-v7a
@xiaochenzhicheng
Copy link

glide使用的是application context,不会用插件的context去找

@ly-android
Copy link

InputStream is = recycleImageView.getResources().openRawResource((int) path);
//fix 插件化中使用的时候,无法找到资源
builder = Glide.with(recycleImageView.getContext()).load(inputStreamToByte(is)).asBitmap();

@aqiuzzt
Copy link

aqiuzzt commented Apr 15, 2021

InputStream is = recycleImageView.getResources().openRawResource((int) path);
//fix 插件化中使用的时候,无法找到资源
builder = Glide.with(recycleImageView.getContext()).load(inputStreamToByte(is)).asBitmap();

####下面是我修复方案####

  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 YYNonOwnedDrawableResource.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 YYDrawableToBitmapConverter.convert(bitmapPool, drawable, width, height);
    }
  1. AppGlideModule注册registerComponents,替换glide解码器
  /* Drawables */
                .prepend(Uri.class, Drawable.class, resourceDrawableDecoder)
                .prepend(
                        Uri.class, Bitmap.class, new YYResourceBitmapDecoder(resourceDrawableDecoder, bitmapPool));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants