2

It was all right while I was testing my widget with emulators but when I try to test it on a real device (HTC Desire 816g), I get the following error when I load an Image from url. This is the error:

05-03 12:15:21.679 24313-24393/com.example.macos.ladderapp D/dalvikvm: Alloc : 11653456
05-03 12:15:21.680 24313-24393/com.example.macos.ladderapp I/dalvikvm: "AsyncTask #2" prio=5 tid=31 RUNNABLE
      | group="main" sCount=0 dsCount=0 obj=0x424d34d0 self=0x637abb60
      | sysTid=24393 nice=10 sched=0/0 cgrp=apps/bg_non_interactive handle=1668947712
      | state=R schedstat=( 93174539 4489846 39 ) utm=9 stm=0 core=1
        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
        at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
05-03 12:15:21.682 24313-24393/com.example.macos.ladderapp I/dalvikvm:     at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:635)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:611)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:649)
        at com.example.macos.ladderapp.RetrieveFeedTask.doInBackground(RetrieveFeedTask.java:57)
        at com.example.macos.ladderapp.RetrieveFeedTask.doInBackground(RetrieveFeedTask.java:25)
        at android.os.AsyncTask$2.call(AsyncTask.java:288)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

And this is the Java code:

protected void onPostExecute(Bitmap bm) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mcontext);
    RemoteViews remoteViews = new RemoteViews(mcontext.getPackageName(), R.layout.simple_widget);
    remoteViews.setImageViewBitmap(R.id.REKLAM, bm);
    appWidgetManager.updateAppWidget(new ComponentName(mcontext, SimpleWidgetProvider.class), remoteViews);

}

@Override
protected Bitmap doInBackground(Context... contexts) {
     Bitmap bm = null;
    try {
        URL aURL = new URL(mLink);
        URLConnection conn = aURL.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        bm = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();
} catch (IOException e) {
    Log.e(TAG, "Error getting bitmap", e);
}
return bm;
}

This is the widget layout file and I think it causes the

Problem loading widget error. I am trying to load an Imageview (REKLAM) and on top left corner of it a clickable arrow vector(clickview).

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/widget_margin"
>

    <ImageView
        android:id="@+id/clickview"
        android:layout_width="50dp"
        android:alpha="0.84"
        android:layout_alignLeft="@id/REKLAM"
        android:src="@drawable/ic_widget_arrow_24dp"
        android:layout_height="40dp" />

    <ImageView
        android:id="@+id/REKLAM"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        />


</RelativeLayout>
keser
  • 2,472
  • 1
  • 12
  • 38

1 Answers1

1

There are a lot of pitfalls when it comes to handling bitmaps manually. I would really advise against rolling your own for general usage.

The library that Google recommends for image processing is Glide. It comes with a handy class specifically for displaying image views in app widgets: AppWidgetTarget.

AppWidgetTarget awt = new AppWidgetTarget(context, R.id.img_dog, remoteViews, appWidgetIds) {
    @Override
    public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
        super.onResourceReady(resource, transition);
    }
};

RequestOptions options = new RequestOptions().
        override(300, 300).placeholder(R.drawable.placeholder_img).error(R.drawable.error_img)


Glide.with(context.getApplicationContext())
        .asBitmap()
        .load(imageUrl)
        .apply(options)
        .into(awt);

In the Gradle the following lines need to be added:

implementation 'com.github.bumptech.glide:glide:4.7.1'
kapt 'com.github.bumptech.glide:compiler:4.7.1'

Glide Samples and Source

Glide Tutorial

But with regards to the exception, this post explains some of the gotchas when working with bitmaps:

Strange out of memory issue while loading an image to a Bitmap object

With Glide, images can be resized with override() shown above.

Also, the 2 close calls:

bis.close();
is.close();

Need to be put in a finally block. Any time close() is called in a try statement whether it is a stream, cursor or another resource it should be a potential red flag. It is almost always better to position those in the finally.

Do Input/Output Streams Get Closed on Destruction

Elletlar
  • 3,136
  • 7
  • 32
  • 38
  • I have updated my question with another issue. Can you help me figure it out? – keser May 03 '18 at 17:24
  • Sure. I'd be happy to take a look. But it might be best to raise it as another issue? Searching for answers on stackoverflow works on 1 to 1 basis. – Elletlar May 03 '18 at 17:51
  • Actually it is still the same question and the same issue. I still got Problem Loading Widget Error. – keser May 03 '18 at 18:32
  • Your XML file is fine. I added it to my widget. Is it possible to put the entire project on github or google drive? I'd be happy to download it in the morning to see what is happening. – Elletlar May 03 '18 at 18:47
  • I wish there was an error message. No warnings or error are there on Logcat. Just widget displays PROBLEM LOADING ERROR on homescreen and it happens only with real devices, on emulators its flawless – keser May 03 '18 at 18:47
  • There are restrictions on the views that can be used in a widget, but the ones that you have RelativeLayout and ImageView are fine...I see quite a few comments that suggest removing the widget and restarting the phone may help with that message :) – Elletlar May 03 '18 at 19:24