Loading Drawable Image Resource in Fresco's SimpleDraweeView

28.4k views Asked by At

I used Fresco 0.5.2:

dependencies {
   compile 'com.facebook.fresco:fresco:0.5.2'
}

I want to use SimpleDraweeView to load a gif image from drawable. Here is my code:

String path = ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+ getResources().getResourcePackageName(R.drawable.default_app) + "/"
+ getResources().getResourceTypeName(R.drawable.default_app) + "/"
+ getResources().getResourceEntryName(R.drawable.default_app);
Uri uri =  Uri.parse(path);
simpleDraweeView =(SimpleDraweeView)
this.findViewById(R.id.simple_drawee_view);
        ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(uri).build();
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(imageRequest)
                .setAutoPlayAnimations(true)
                .build();
        simpleDraweeView.setController(controller);

The default_app is just a jpeg image and it doesn't work.

I used this as the Fresco's documentation mentioned. Is there any problem with the path or code?

7

There are 7 answers

3
Sebastian On BEST ANSWER

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.

0
Alireza Ghanbarinia On

Try using this:

UriUtil.getUriForResourceId(resId)

3
lfmn On

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.** { *; }
0
zOqvxf On

Note that an XML drawable cannot be directly used as the image in a SimpleDraweeView.

But as the documentation states, "If you want to display an XML drawable as the main image, then set it as a placeholder and use the null uri."

draweeView.setImageURI(null as String?)
draweeView.hierarchy.setPlaceholderImage(drawableImage)
0
Atiq On

You can use

draweeView.setActualImageResource(R.drawable.background);

0
leocadiotine On

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.

0
Estel On

There's an implementation of NoActivity.java's code in the Fresco library:

ImageRequestBuilder.newBuilderWithResourceId(R.drawable.resId).build()