Reputation: 2614
I am trying to use vector drawable in my widget bt on pre-lollipop, the image not displays, as I tried displaying image programmatically bcoz app:srcCompat don't work in case of widgets, based on the answers given in the following:
AppCompat 23.2 use VectorDrawableCompat with RemoteViews (AppWidget) on API<21
but after using the above approach I end up with this stack trace:
02-11 18:55:12.686 12176-12176/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.mygarden, PID: 12176
java.lang.RuntimeException: Unable to start receiver com.example.android.mygarden.PlantWidgetProvider: android.content.res.Resources$NotFoundException: File res/drawable/water_drop_blue.xml from drawable resource ID #0x7f08008d
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2580)
at android.app.ActivityThread.access$1700(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/water_drop_blue.xml from drawable resource ID #0x7f08008d
at android.content.res.Resources.loadDrawable(Resources.java:2152)
at android.content.res.Resources.getDrawable(Resources.java:710)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:353)
at com.example.android.mygarden.PlantWidgetProvider.updateAppWidget(PlantWidgetProvider.java:49)
at com.example.android.mygarden.PlantWidgetProvider.onUpdate(PlantWidgetProvider.java:76)
at android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:66)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2573)
at android.app.ActivityThread.access$1700(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:933)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
at android.content.res.Resources.loadDrawable(Resources.java:2148)
at android.content.res.Resources.getDrawable(Resources.java:710)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:353)
at com.example.android.mygarden.PlantWidgetProvider.updateAppWidget(PlantWidgetProvider.java:49)
at com.example.android.mygarden.PlantWidgetProvider.onUpdate(PlantWidgetProvider.java:76)
at android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:66)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2573)
at android.app.ActivityThread.access$1700(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
I do have my VectorDrawable in drawable/water_drop_blue.xml, bt it shows however Resources$NotFoundException. Here is my updateAppWidget method:
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
// Create an Intent to launch MainActivity when clicked
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent
.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.plant_widget);
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
views.setImageViewResource(R.id.widget_water_button, R.drawable.water_drop_blue);
} else {
Drawable drawable = ContextCompat.getDrawable(context, R.drawable.water_drop_blue);
drawable = (DrawableCompat.wrap(drawable)).mutate();
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
drawable.setBounds(0, 0, c.getWidth(), c.getHeight());
drawable.draw(c);
views.setImageViewBitmap(R.id.widget_water_button, bitmap);
}
My app widget layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/widget_water_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<ImageView
android:id="@+id/widget_plant_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/grass" />
</LinearLayout>
I have also included in app level build.gradle file
vectorDrawables.useSupportLibrary = true
Please help me in displaying Vector in widget on pre-lollipop devices. I also tried using AppCompatImageView bt it didn't work.
Upvotes: 0
Views: 900
Reputation: 1746
in my project it's something like this:
remoteViews.setImageViewBitmap(R.id.widget_control, VectorUtil
.vectorToBitmap(context, R.drawable.chevron_left));
VectorUtils:
public static Bitmap vectorToBitmap(Context context, @DrawableRes int resVector) {
Drawable drawable = AppCompatResources.getDrawable(ctx, resVector);
Bitmap b = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
drawable.setBounds(0, 0, c.getWidth(), c.getHeight());
drawable.draw(c);
return b;
}
And it works fine.
But as i can see in your stacktrace it's problem with your vector, because of Tag that Parser of vector can't read it. Validate your vector.
Upvotes: 3