Loading Drawable Image Resource in Fresco's SimpleDraweeView

To get the URI of the resource image to be loaded in Fresco, use "res:/" instead of ContentResolver.SCHEME_ANDROID_RESOURCE, which is used for URIs in a normal case.

import com.facebook.common.util.UriUtil;

Uri uri = new Uri.Builder()
    .scheme(UriUtil.LOCAL_RESOURCE_SCHEME) // "res"
    .path(String.valueOf(resId))
    .build();
// uri looks like res:/123456789
simpleDraweeView.setImageURI(uri);

You should be able to use that URI with a DraweeController too.

Here are Fresco’s supported URIs.


I'm using Fresco 0.7.0

ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithResourceId(R.drawable.image).build();
SimpleDraweeView draweeView = (SimpleDraweeView) v.findViewById(R.id.picture);
draweeView.setImageURI(imageRequest.getSourceUri());

If you are using proguard you need to add these lines in your proguard file:

-keep class com.facebook.imagepipeline.gif.** { *; }
-keep class com.facebook.imagepipeline.webp.** { *; }

Try using this:

String path = "res:/" + R.drawable.default_app; // Only one slash after res:
simpleDraweeView.setImageURI(Uri.parse(path));

No need to use ImageRequest or DraweeController.

Tags:

Android

Fresco