1

有没有办法将图像从互联网延迟加载到远程视图中。appwidget中的lazyload图像

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

我使用此功能,但它在很短的时间内阻止了我的小部件。

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; 
} 

感谢

回答

3

绝对

  1. 画出你的插件的常规方式,所有的文字部分,图像等
  2. 旁边创建一个将载入图片的服务。 Here's good tutorial,其中包括如何创建并从appwidget调用服务
  3. 更新您的小部件后调用该服务。通过小部件ID和图像URL
  4. 从缓存或远程加载图像到您的服务中,并再次更新您的小部件。瞧,你现在有它
+0

如果没有这两个更新之间再次更新只小部件。在这种情况下,您将失去新的窗口小部件状态 – Maxim

+0

还有一件事,您需要在将图像展示到窗口小部件之前向下取样您的图像,否则您将被“RemoteViews用于窗口小部件更新超出最大位图内存使用情况例外”的冲击。 – TilalHusain

+0

@Bostone如果你需要加载appWidget中的listView的图像?根据我所看到的,这是在“getViewAt”(在BG线程上运行)中完成的,但在加载图像之前无法更新其余行(文本等),这太糟糕了...... –