ABausG / home_widget

Flutter Package for Easier Creation of Home Screen Widgets
786 stars 218 forks source link

šŸž [Bug] Exception Invalid image dimensions. #252

Closed moha-b closed 4 months ago

moha-b commented 7 months ago

On the home widget, the temperature and date display correctly, but the image does not. I followed the example explanation and here is a code snippet.

What I follow

https://github.com/ABausG/home_widget/blob/365f8f910bccf577b0103a239d26d8e3c175e06e/example/lib/main.dart#L118-L125

Dart Code

try {
            print(state.weather.theme.image);
            await Future.wait([
              HomeWidget.saveWidgetData('date', state.weather.date),
              HomeWidget.saveWidgetData(
                  'temperature', state.weather.temperature.toString()),
              HomeWidget.updateWidget(androidName: 'WeatherWidget'),
              HomeWidget.renderFlutterWidget(
                Image.asset(state.weather.theme.image),
                key: 'image',
              ),
            ]);
          } on PlatformException catch (exception) {
            log("Error Sending Data", error: exception);
          }

Log

I/flutter (31368): assets/images/weather/Day Sun.webp    // <- the image exist
E/BitmapFactory( 2649): Unable to decode stream: java.io.FileNotFoundException: Image: open failed: ENOENT (No such file or directory)
I/AppWidgetManager(31368): updateAppWidget() appWidgetIds = [29]
E/flutter (31368): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Exception: Failed to render the widget: Exception: Invalid image dimensions.
E/flutter (31368): #0      HomeWidget.renderFlutterWidget (package:home_widget/src/home_widget.dart:264:7)
E/flutter (31368): <asynchronous suspension>
E/flutter (31368): #1      Future.wait.<anonymous closure> (dart:async/future.dart:518:21)
E/flutter (31368): <asynchronous suspension>
E/flutter (31368): #2      HomeScreen.build.<anonymous closure> (package:clima/features/home/screens/home_screen.dart:23:13)
E/flutter (31368): <asynchronous suspension>
E/flutter (31368): 

What I follow

https://github.com/ABausG/home_widget/blob/365f8f910bccf577b0103a239d26d8e3c175e06e/example/android/app/src/main/kotlin/es/antonborri/home_widget_example/HomeWidgetExampleProvider.kt#L38-L44

Native Code

 appWidgetIds.forEach { widgetId ->
            val views = RemoteViews(context.packageName,R.layout.weather_widget).apply {
                val widgetData = HomeWidgetPlugin.getData(context)
                val temp = widgetData.getString("temperature","Temperature")
                val date = widgetData.getString("date","Date")
                val image = widgetData.getString("image", null)
                if (image != null) {
                    setImageViewBitmap(R.id.image, BitmapFactory.decodeFile(image))
                    setViewVisibility(R.id.image, View.VISIBLE)
                } else {
                    setViewVisibility(R.id.image, View.GONE)
                }
                setTextViewText(R.id.temperature,temp)
                setTextViewText(R.id.date,date)
            }
            appWidgetManager.updateAppWidget(widgetId,views)
        }

Widget Info

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:description="@string/app_widget_description"
    android:initialKeyguardLayout="@layout/weather_widget"
    android:initialLayout="@layout/weather_widget"
    android:minWidth="40dp"
    android:minHeight="40dp"
    android:previewImage="@drawable/example_appwidget_preview"
    android:previewLayout="@layout/weather_widget"
    android:resizeMode="horizontal|vertical"
    android:targetCellWidth="3"
    android:targetCellHeight="2"
    android:updatePeriodMillis="86400000"
    android:widgetCategory="home_screen" />

I understand that the image may be large, but there are no restrictions in the documentation.

image

The Problem is the image is not appearing even if I resize it

HomeWidget.renderFlutterWidget(
                Image.asset(
                  state.weather.theme.image,
                  width: 100,
                  height: 100,
                ),
                key: 'image',
              ),

The code is no longer throwing an Exception, but the image is still not appearing.

WhatsApp Image 2024-04-21 at 07 54 26_212c4836

Native Ui

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/Widget.Android.AppWidget.Container"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:padding="16dp"
    >

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:layout_margin="8dp"
        android:contentDescription="@string/weather_image" />
    <TextView
        android:id="@+id/temperature"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:contentDescription="@string/temperature"
        android:textSize="24sp"
        android:textStyle="bold|italic" />
    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:contentDescription="@string/date"
        android:textSize="16sp"
        android:textStyle="bold|italic" />
</LinearLayout>
ABausG commented 4 months ago

Closing this as a duplicate of #178