J.Doe
J.Doe

Reputation: 366

AppWidgetProvider takes resources from wrong package (Lollipop only)

I have an app widget that shows an image.

Uri uri = Uri.parse("android.resource://" + packageName + "/" + resId);
rv = new RemoteViews(defaultPackageName, widgetLayout);
rv.setImageViewUri(R.id.image, uri);

Images are stored in different packages under drawable directory, i am getting its resource id like so:

Resources r = getPackageManager().getResourcesForApplication(otherPackage);
int resId = r.getIdentifier("image_1", "drawable", otherPackage);

Everything works correctly until the image_1 is an xml bitmap, that has an android:src tag:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/pat1"
        android:tileMode="repeat"/>

After a long research, what i found out and what i have tried:

  1. The problem occurs only in lollipop (5.0, 5.1 etc...), and only in AppWidgetProvider (it works correctly if i use this resource inside activity).

  2. It is neither working with other methods like:

    rv.setImageViewBitmap(R.id.image, BitmapFactory.decodeResource(r, resId));
    
  3. It is getting the right resource id of @drawable/pat1, but taking the drawable with the same res id from default package. (I have checked that in R.java in both packages) If there is no resource with that id, it just puts nothing inside R.id.image, and the widgets appears to be blank.

  4. I have tried to hardcode the package name in image_1.xml like so:

    android:src="@com.other.package.name:drawable/pat1"
    

    wasn't helpful.

  5. The problem occurs on a real devices with different roms and launchers, as well as on an emulator.

I hope i was clear, and hope someone faced similar problem and can share a solution or suggest more workarounds.

Upvotes: 0

Views: 100

Answers (1)

J.Doe
J.Doe

Reputation: 366

It seems to be a bug in Lollipop, so i solved that by a workaround. I just created similar layouts in all my packages, inside each layout there is an ImageView with needed drawable. Finally i let my RemoteViews create itself from selected layout.

RemoteViews rv = new RemoteViews(otherPackage, layoutResId);

Upvotes: 2

Related Questions