Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28541

Loading images from application assets with Glide library

I have switched my current project from Picasso to Glide. However, I cannot manage to load an image from the application assets. Is that even possible?

Sample code:

String assetPath = "file:///android_asset/flags/FR.jpg";
Glide.with(getContext())
            .load(Uri.parse(assetPath))
            .placeholder(missingFlagDrawable)
            .centerCrop()
            .crossFade()
            .into(flag);

I have also tried the load(String) method without success and I don't see a load method taking for instance an InputStream or a FileDescriptor I could have obtained with the AssetManager class.

That exact same code was working in Picasso.

Glide is working properly in the rest of the app and loading nicely remote images.

Upvotes: 3

Views: 8238

Answers (2)

Carlos Daniel
Carlos Daniel

Reputation: 2699

I prefer to put the images in raw directory instead of assets, then build de uri as follows:

fun setImageFromRaw(imageView: ImageView, rawId: Int) {
        val context = imageView.context
        val uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.packageName + "/" + rawId)
        GlideApp.with(context).load(uri).into(imageView)
    }

This option is based on what Glide's team has done in samples repository.

Upvotes: 0

Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28541

Works with Glide.with(xxx).load(xxx).asBitmap()

As stated in the GitHub issue I had opened at the time I faced that issue.

Upvotes: 4

Related Questions