Fresco: Fresco crashing on using SVG as placeHolder and failure images prior to lollipop

Created on 30 Apr 2016  路  14Comments  路  Source: facebook/fresco

Since I upgraded to support library 23.3.0. I am using only vector drawables and not png assets. Working fine on lollipop and above but crashing prior to lollipop.

enhancement

Most helpful comment

placeholderImage with vector drawable in XML doesn't work on API 19 i.e Android KitKat

All 14 comments

Hi!

Update: See my post below on how you can use vector drawables with Fresco.

Original comment:
We currently do not support vector drawables for API < 21.
It works for API 21 and above because Android added native vector drawable support with Lollipop. The support library internally replaces your ImageView with AppCompatImageView (which supports vectors pre-Lollipop) but it can't do this for DraweeView.
You can also take a look at https://medium.com/@chrisbanes/appcompat-v23-2-age-of-the-vectors-91cbafa87c88#.hwhg5l1rd for more information.

Okay. Any plans to do this in near future.
I am a huge fan of fresco image loading and want to make use of xml resources to reduce app size.
I don't want to move to any other library.

A bit of a clarification there: By "We currently do not support vector drawables for API < 21." I meant we do not support vector drawables if specified in XML.
You can of course still us Fresco with support vector drawables that should work on all supported Android versions by building your own drawee hierarchy in Java.

A quick example for this would be:

// android.support.v4.content.ContextCompat creates your vector drawable
Drawable placeholderDrawable = ContextCompat.getDrawable(context, R.drawable.yourVectorAsset);

SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.yourImage);

// Set the placeholder image to the placeholder vector drawable
draweeView.setHierarchy(
    GenericDraweeHierarchyBuilder.newInstance(context.getResources())
        .setPlaceholderImage(placeholderDrawable)
        .build());

I just tested this and it also works for me if I define the placeholder in XML

<com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/drawee"
    ...
    fresco:placeholderImage="@drawable/vector_graphic"
    />

with

compile 'com.android.support:appcompat-v7:23.2.1'

and

android {
    defaultConfig { 
        vectorDrawables.useSupportLibrary = true  
    }
}

in my build.gradle.

Thank you so much. This worked for me.

Is it working before lollipop?

It works for me on Jelly Bean, so yes.

When I use this I get error in inflating on simple drawee view.

Here is the stacktrace:

android.view.InflateException: Binary XML file line #23: Error inflating class <unknown> at android.view.LayoutInflater.createView(LayoutInflater.java:623) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:672) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697) at android.view.LayoutInflater.rInflate(LayoutInflater.java:758) at android.view.LayoutInflater.inflate(LayoutInflater.java:495) at android.view.LayoutInflater.inflate(LayoutInflater.java:400) at com.listup.android.adapter.BaseRecyclerAdapter.onCreateViewHolder(BaseRecyclerAdapter.java:23) at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5482) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4707) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994) at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353) at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574) at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283) at android.view.View.layout(View.java:14845) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055) at android.view.View.layout(View.java:14845) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) at android.view.View.layout(View.java:14845) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.support.v7.widget.RecyclerView$LayoutManager.layoutDecorated(RecyclerView.java:7650) at android.support.v7.widget.StaggeredGridLayoutManager.layoutDecoratedWithMargins(StaggeredGridLayoutManager.java:1757) at android.support.v7.widget.StaggeredGridLayoutManager.fill(StaggeredGridLayoutManager.java:1628) at android.support.v7.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:657) at android.support.v7.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:592) at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283) at android.view.View.layout(View.java:14845) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) at android.widget.FrameLayout.onLayout(FrameLayout.java:388) at android.view.View.layout(View.java:14845) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1695) at android.view.View.layout(View.java:14845) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:122) at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42) at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1170) at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:814) at android.view.View.layout(View.java:14845) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.widget.FrameLayout.l

I have added this in my build.gradle

   ` resConfigs "en"`

On removing it voila magic. Working with XML attributes too.

Great!

You can add custom SVG decoders and drawable factories now. A very simple example how this could look like can be found here: https://github.com/facebook/fresco/tree/master/samples/showcase/src/main/java/com/facebook/fresco/samples/showcase/imageformat/svg

placeholderImage with vector drawable in XML doesn't work on API 19 i.e Android KitKat

My problem was extending FragmentActivity as the parent activity, so when I was trying to inflate the layout using its context it didn't work. So I changed it to AppCompatActivity and now its working just fine!

you have to enable this parameter by default it is not enabled in support library used by your application.
you have put this in onCreate of Application class.
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

Was this page helpful?
0 / 5 - 0 ratings