1

i have a Widget in my app which have a ImageView for displaying a canvas.

If i look into my User Crashs i see that error very frequently (but always just on a few devices):

 java.lang.RuntimeException: Unable to start receiver my.package.name.WidgetProvider: java.lang.IllegalArgumentException: RemoteViews for widget update exceeds maximum bitmap memory usage (used: 5715000, max: 5529600 /*these arent always the same :)*/ ) The total memory cannot exceed that required to fill the device's screen once.

my canvas has a dynamic size, it automatically resizes when the user resizes, and has a specified aspect ratio, it is created like that:

Resources r = context.getResources();
    SharedPreferences widgets = context.getSharedPreferences("widgets", 0);

            //these are stored in onAppWidgetOptionsChanged when user resizes his widget
    int w = widgets.getInt(widgetId + "_width", 130);
    int h = widgets.getInt(widgetId + "_height", 160);

    w = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, w,
            r.getDisplayMetrics());
    h = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, h,
            r.getDisplayMetrics());

    float scaleX = (float) w / (float) 13;
    float scaleY = (float) h / (float) 16;

    float scale = 1.5f * (scaleX < scaleY ? scaleX : scaleY);//i multiplied it with 1.5 because the canvas was very unsharp

    w = (int) (13 * scale);
    h = (int) (16 * scale);

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
            R.layout.widget);


    Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
    Canvas canvas = new Canvas(bmp);


 [...]

    remoteViews.setImageViewBitmap(R.id.widget, bmp);

    appWidgetManager.updateAppWidget(widgetId, remoteViews);

i guess it is because canvas is 1.5x larger than the widget, but else it is very unsharp.

What can i do?

Thanks in advance.

Metin Kale

1 Answer 1

1

The documentation specifies that:

The total Bitmap memory used by the RemoteViews object cannot exceed that required to fill the screen 1.5 times, ie. (screen width x screen height x 4 x 1.5) bytes.

So, use that to control your bitmap size and then use ImageView scaleType fitCenter in your layout to scale-to-fit the bitmap in the widget.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.