1

Is there a way to lazyload an image from the internet into a remoteview.

remoteView.setImageViewBitmap(R.id.image, UrlUtils.loadBitmap(bitmapUrl));

I use this function but it is blocking my widget during a small time.

public static Bitmap loadBitmap(String url) {
    Bitmap bitmap = null;
    InputStream in = null;
    BufferedOutputStream out = null;

    try {

        in = new BufferedInputStream(getPageInputStream(url));

        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream);
        Utils.copy(in, out);
        out.flush();

        final byte[] data = dataStream.toByteArray();
        BitmapFactory.Options options = new BitmapFactory.Options();
        // options.inSampleSize = 1;

        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,
                options);

        in.close();
        out.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

    return bitmap;
}

Thanks

yostane
  • 383
  • 1
  • 4
  • 19

2 Answers2

3

Absolutely

  1. Draw your widget the usual way, all the textual parts, etc. beside image
  2. Create a service which will load image. Here's good tutorial that includes how to create and call service from the appwidget
  3. After updating your widget call the service. Pass widget ID and image URL
  4. Load image from cache or remotely in your service and update your widget again. Voila, you have it now
Bostone
  • 36,858
  • 39
  • 167
  • 227
  • If only widget was not updated again between these two updates. In that case you will lose new widget state – Maxim Mar 19 '12 at 16:10
  • One more thing, you need to down sample your image before showing it into the widget or else you will be stormed by `RemoteViews for widget update exceeds maximum bitmap memory usage` exceptions. – TilalHusain Jan 01 '14 at 07:35
  • @Bostone what if you need to load the images for a listView in the appWidget? According to what I see, this is done in the "getViewAt" (runs on BG thread), but there is no way to update the rest of the row (text etc) before loading the image, which is too bad... – android developer May 18 '15 at 10:59
1

Try android-query lib for lazyload loading.

https://code.google.com/p/android-query/#Image_Loading

ohmrefresh
  • 11
  • 1
  • 1