-1

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

3
  • See also the Glide documentation, which shows that there is a version of load() that takes a File. Commented May 13, 2024 at 19:29
  • @CommonsWare The linked solution did not solve my issue. I updated the question. Commented May 13, 2024 at 19:45
  • The current Glide documentation doesn't mention a native SVG import/loading method. From the error logs it seems like you're loading SVG (xml/vector) with a method expecting a raster (bitmap) image binary data chunk . Otherwise you could probably convert your SVG to a android drawable beforehand. This SO post may be helpful "How to load svg image to image view using Glide" Commented May 13, 2024 at 22:57

1 Answer 1

0

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();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.