当前位置: 首页 > 知识库问答 >
问题:

在Android上加载svg资源失败

锺离赤岩
2023-03-14

在projectimplementation'com中使用。github。邦普泰克。滑动:滑动:4.8.0'

从服务器链接(.svg)获取

如何下载该。svg给定url的图标,并使用glide在ImageView中显示它?

现在我用的是这样的:

Glide.with(bankLogoView.context)
            .`as`(PictureDrawable::class.java)
            .load(data.logoUrl)
            .listener(SvgSoftwareLayerSetter())
            .apply(options)
            .into(bankLogoView)

在哪里有:

private val options = RequestOptions().centerCrop()
        .diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.drawable.ic_logo_splash)

还添加了下一个类SvgDecoder

class SvgDecoder : ResourceDecoder<InputStream, SVG> {

override fun handles(source: InputStream, options: Options): Boolean {
    // TODO: Can we tell?
    return true
}

@Throws(IOException::class)
override fun decode(source: InputStream, width: Int, height: Int,
                    options: Options): Resource<SVG>? {
    try {
        val svg = SVG.getFromInputStream(source)
        return SimpleResource(svg)
    } catch (ex: SVGParseException) {
        throw IOException("Cannot load SVG from stream", ex)
    }
}
}

SVGDRAWABLE转码器:

class SvgDrawableTranscoder : ResourceTranscoder<SVG, PictureDrawable> {

override fun transcode(toTranscode: Resource<SVG>,
                       options: Options): Resource<PictureDrawable>? {
    val svg = toTranscode.get()
    val picture = svg.renderToPicture()
    val drawable = PictureDrawable(picture)
    return SimpleResource(drawable)
}
}

SvgMoules:

@GlideModule
class SvgModule : AppGlideModule() {
override fun registerComponents(context: Context, glide: Glide,
                                registry: Registry) {
    registry.register(SVG::class.java, PictureDrawable::class.java, SvgDrawableTranscoder())
            .append(InputStream::class.java, SVG::class.java, SvgDecoder())
}

// Disable manifest parsing to avoid adding similar modules twice.
override fun isManifestParsingEnabled(): Boolean {
    return false
}
}

SvgSoftwareLayerSetter:

class SvgSoftwareLayerSetter : RequestListener<PictureDrawable> {

override fun onLoadFailed(e: GlideException?, model: Any, target: Target<PictureDrawable>,
                          isFirstResource: Boolean): Boolean {
    val view = (target as ImageViewTarget<*>).view
    view.setLayerType(ImageView.LAYER_TYPE_NONE, null)
    return false
}

override fun onResourceReady(resource: PictureDrawable, model: Any,
                             target: Target<PictureDrawable>, dataSource: DataSource, isFirstResource: Boolean): Boolean {
    val view = (target as ImageViewTarget<*>).view
    view.setLayerType(ImageView.LAYER_TYPE_SOFTWARE, null)
    return false
}
}

但我也只显示选项<代码>数据。logoUrl不为空,但不显示

UPD:

W/Glide:加载失败https://mandarine.com/logos/providers/xf/fake_demobank_xf.svg大小为[65x66]类com.bumptech.glide.load.engine.Glide异常:加载资源失败

当我时,试图从谷歌硬编码一些url。加载(“url”)就像:

https://upload.wikimedia.org/wikipedia/sco/7/71/Pringles.svg

但也没有显示图像。

我尝试做的第二件事:实现这个库

implementation 'com.caverock:androidsvg-aar:1.3'

然后修改了我的布局:

<com.caverock.androidsvg.SVGImageView
        android:id="@+id/bankLogoView"
        style="@style/icon_account_view"
        app:svg="@drawable/ic_buy_violet"/>

在我的支架上,像这样:

bankLogoView.setImageAsset("wallet.svg")

但我如何才能html" target="_blank">下载。来自服务器的svg文件?

共有2个答案

郎雪风
2023-03-14

这就做到了。从Glide v4开始,解码器和代码转换器不必为每个请求创建,并且在GlideMoules中设置一次就好了。输入流的URL由默认的ModelLoaders之一Glide处理。

@GlideModule
class GlideModule : AppGlideModule() {
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
    registry.append( InputStream::class.java, SVG::class.java, SvgDecoder() )
    registry.register( SVG::class.java, PictureDrawable::class.java, SvgDrawableTranscoder() )
    }
}

class SvgDecoder : ResourceDecoder<InputStream, SVG> {
override fun handles(source: InputStream, options: Options): Boolean {
    return true
}

@Throws(IOException::class)
override fun decode(source: InputStream, width: Int, height: Int, options: Options): Resource<SVG> {
    try {
        val svg = SVG.getFromInputStream(source)
        return SimpleResource(svg)
    } catch (ex: SVGParseException) {
        throw IOException("Cannot load SVG from stream", ex)
    }
}
}

class SvgDrawableTranscoder : ResourceTranscoder<SVG, PictureDrawable> {
    override fun transcode(toTranscode: Resource<SVG>, options: Options): Resource<PictureDrawable> {
    val svg = toTranscode.get()
    val picture = svg.renderToPicture()
    val drawable = PictureDrawable(picture)
    return SimpleResource(drawable)
    }
}

    GlideApp.with(this)
            .load("https://sample.svg" )
            .into(imageView)
孟佑运
2023-03-14

我的代码在Java但希望你能收到备忘录。将以下依赖项添加到应用模块build.gradle文件:

implementation 'com.caverock:androidsvg:1.2.1'

在您的GlideMoules中:

public class GlideModule extends AppGlideModule {
    ...
    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
        registry.register(SVG.class, PictureDrawable.class, new SvgDrawableTranscoder()).append(InputStream.class, SVG.class, new SvgDecoder());
    }
}

SvgDecoder。JAVA

public class SvgDecoder implements ResourceDecoder<InputStream, SVG> {

    @Override
    public boolean handles(InputStream source, Options options) throws IOException {
        // TODO: Can we tell?
        return true;
    }

    public Resource<SVG> decode(InputStream source, int width, int height, Options options)
            throws IOException {
        try {
            SVG svg = SVG.getFromInputStream(source);
            return new SimpleResource<SVG>(svg);
        } catch (SVGParseException ex) {
            throw new IOException("Cannot load SVG from stream", ex);
        }
    }
}

SVGDrawable转码器。JAVA

public class SvgDrawableTranscoder implements ResourceTranscoder<SVG, PictureDrawable> {
    @Override
    public Resource<PictureDrawable> transcode(Resource<SVG> toTranscode, Options options) {
        SVG svg = toTranscode.get();
        Picture picture = svg.renderToPicture();
        PictureDrawable drawable = new PictureDrawable(picture);
        return new SimpleResource<PictureDrawable>(drawable);
    }
}

SvgSoftwareLayerSetter。JAVA

public class SvgSoftwareLayerSetter implements RequestListener<PictureDrawable> {

    @Override
    public boolean onLoadFailed(GlideException e, Object model, Target<PictureDrawable> target,
                                boolean isFirstResource) {
        ImageView view = ((ImageViewTarget<?>) target).getView();
        view.setLayerType(ImageView.LAYER_TYPE_NONE, null);
        return false;
    }

    @Override
    public boolean onResourceReady(PictureDrawable resource, Object model,
                                   Target<PictureDrawable> target, DataSource dataSource, boolean isFirstResource) {
        ImageView view = ((ImageViewTarget<?>) target).getView();
        view.setLayerType(ImageView.LAYER_TYPE_SOFTWARE, null);
        return false;
    }
}

然后将其与Glide一起使用,如:

Glide.with(this)
        .as(PictureDrawable.class)
        .load(svgUrl)
        .listener(new SvgSoftwareLayerSetter())
        .into(imageView);
 类似资料:
  • 我正试图通过Glide将图像加载到。但是图像没有加载-我得到一个错误。我正在使用以下代码 日志

  • 我正在使用从资源加载的自定义字体。此代码在Eclipse中完美运行,但在作为jar运行时会出现错误。 作为jar运行时,会出现以下错误(从命令提示符窗口): 第83行如上述注释所示

  • 我还试图将该文件的权限更改为777,但它不起作用。

  • 浏览器允许我们跟踪外部资源的加载 —— 脚本,iframe,图片等。 这里有两个事件: onload —— 成功加载, onerror —— 出现 error。 加载脚本 假设我们需要加载第三方脚本,并调用其中的函数。 我们可以像这样动态加载它: let script = document.createElement('script'); script.src = "my.js"; docume

  • CodeIgniter 的"自动加载"特性可以允许系统每次运行时自动初始化类库、辅助函数和模型。 如果你需要在整个应用程序中全局使用某些资源,为方便起见可以考虑自动加载它们。 支持自动加载的有下面这些: libraries/ 目录下的核心类 helpers/ 目录下的辅助函数 config/ 目录下的用户自定义配置文件 system/language/ 目录下的语言文件 models/ 目录下的模

  • 传统方式的导入外部JS和CSS文件的方法是直接在模板文件使用: <script type='text/javascript' src='/static/js/common.js'> <link rel="stylesheet" type="text/css" href="/static/css/style.css" /> 系统提供了专门的标签来简化上面的导入: {load href="/stati