Reputation: 2642
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>
Upvotes: 2
Views: 351
Reputation: 3234
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'
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
Upvotes: 1