javaandroidandroid-glide

Glide: Load local SVG file into ImageView


I have a local SVG file that I want to load into my image view. The SVG file is provided to my app thru our integration mechanism in the company. In other words, I cannot get the SVG file thru the form of URL. I am using Glide to load the image but could not make it work. Here is my setup

This is my dependency:

implementation 'com.github.bumptech.glide:glide:4.16.0'

This is my permission:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

And here is my source code:

@Override
protected void onCreate(Bundle savedInstanceState) {
...
    File imageFile = // i get an instance of SVG file from our integration module
    ImageView imageView = findViewById(R.id.imageview);
    Glide.with(this).load(imageFile .toURI()).into(imageView);
}

And finally, the error:

Cause (1 of 1): class com.bumptech.glide.Registry$NoModelLoaderAvailableException: Failed to find any ModelLoaders registered for model class: class java.net.URI 2024-05-13 23:22:41.654 23538-23538/com.sample.app I/Glide: Root cause (1 of 1) com.bumptech.glide.Registry$NoModelLoaderAvailableException: Failed to find any ModelLoaders registered for model class: class java.net.URI

EDIT:

I tried what was discussed in here but still got error. This is my resulting code:

@Override
protected void onCreate(Bundle savedInstanceState) {
...
    File imageFile = // i get an instance of SVG file from our integration module
    ImageView imageView = findViewById(R.id.imageview);
    Glide.with(this).load(imageFile).into(imageView);
}

And this is the resulting error:

2024-05-13 23:39:38.605 26958-26958/com.sample.app W/Glide: Load failed for [/data/data/com.sample.app/files/parameters/sample.svg] with dimensions [720x1102] class com.bumptech.glide.load.engine.GlideException: Failed to load resource There were 2 root causes: java.lang.RuntimeException(setDataSource failed: status = 0x80000000) java.lang.RuntimeException(setDataSource failed: status = 0x80000000) call GlideException#logRootCauses(String) for more detail Cause (1 of 3): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{DirectByteBuffer->Object->Drawable}, LOCAL Cause (1 of 3): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{DirectByteBuffer->GifDrawable->Drawable} Cause (2 of 3): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{DirectByteBuffer->Bitmap->BitmapDrawable} Cause (3 of 3): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{DirectByteBuffer->BitmapDrawable->Drawable} Cause (2 of 3): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{FileInputStream->Object->Drawable}, LOCAL Cause (1 of 3): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{FileInputStream->GifDrawable->Drawable} Cause (2 of 3): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{FileInputStream->Bitmap->BitmapDrawable} Cause (3 of 3): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{FileInputStream->BitmapDrawable->Drawable} Cause (3 of 3): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{ParcelFileDescriptor->Object->Drawable}, LOCAL


Solution

  • This solved my issue.

    app/build.gradle:

    implementation 'com.github.bumptech.glide:glide:4.16.0'
    implementation("com.caverock:androidsvg:1.4")
    

    Java code:

    File imageFile = // i get an instance of SVG file from our integration module
    ImageView imageView = findViewById(R.id.imageview);
    try {
        SVG svg = SVG.getFromInputStream(new FileInputStream(imageFile));
        Drawable drawable = new PictureDrawable(svg.renderToPicture());
        Glide.with(this).load(drawable).into(imageView);
    } catch (SVGParseException | FileNotFoundException e) {
        e.printStackTrace();
    }