21

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?

Sebastian
  • 2,896
  • 23
  • 36
Freddy
  • 764
  • 2
  • 6
  • 21

7 Answers7

61

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.

Community
  • 1
  • 1
Sebastian
  • 2,896
  • 23
  • 36
  • I'm using Fresco 0.7.0 and 0.8.1, both can't load `res:/` URI. Any idea? – Hieu Rocker Nov 13 '15 at 09:13
  • 1
    This kinda works, but be careful you also have to clear the cache for that Uri, in case you have different images for different configurations (like landscape vs portrait) – nbarraille Jan 08 '16 at 22:33
  • 1
    Same code is written in following method `UriUtil.getUriForResourceId(resId)` so can use directly – Sumit Jain Jun 09 '20 at 07:51
22

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.** { *; }
lfmn
  • 396
  • 3
  • 6
  • Why would the ProGuard lines be necessary? Why would ProGuard remove them? – Bam May 12 '16 at 09:30
  • At that time these lines were necessary to get Fresco 0.7.0 working with proguard. See [error here](http://stackoverflow.com/questions/33867642/proguard-error-with-fresco/34351360#34351360). Now for Fresco 0.10.0 maybe it's not necessary anymore. See [proguard config here](https://raw.githubusercontent.com/facebook/fresco/master/proguard-fresco.pro). – lfmn May 14 '16 at 16:13
  • I don't think so, but maybe developers will add this soon. See [here](https://github.com/facebook/fresco/issues/1008) and [here](https://github.com/facebook/fresco/pull/1386) – lfmn Sep 12 '16 at 18:06
9

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.

leocadiotine
  • 3,295
  • 2
  • 22
  • 19
4

Try using this:

UriUtil.getUriForResourceId(resId)

4

You can use

draweeView.setActualImageResource(R.drawable.background);

Atiq
  • 14,435
  • 6
  • 54
  • 69
1

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

ImageRequestBuilder.newBuilderWithResourceId(R.drawable.resId).build()
Estel
  • 2,154
  • 2
  • 18
  • 25
0

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)
zOqvxf
  • 1,469
  • 15
  • 18