2012-12-27 36 views
0

我無法解決如何將圖像下載到我的小工具!Android:小工具加載圖像

在我的小部件中,我使用url,title加載AsyncTask json,然後在TextView中顯示標題,我需要從url加載圖像。

我試着用這個,但圖片的加載,現在顯示

class LoadImages extends AsyncTask<Void, Void, Bitmap>{ 
     @Override 
     protected Bitmap doInBackground(Void... params) { 
      Bitmap bitmap = null; 
      InputStream in = null; 
      BufferedOutputStream out = null; 

      try { 
       in = new BufferedInputStream(new URL("http://mysite/simple.img").openStream()); 

       final ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); 
       out = new BufferedOutputStream(dataStream); 
       out.flush(); 

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

       bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options); 
       Log.e("Log", "Yeah"); 
      } catch (IOException e) { 
       Log.e("Log", "Could not load Bitmap from: " + "mysiteg"); 
      } 

      return bitmap; 
     } 
    } 

和update我把這個

 LoadImages load = new LoadImages(); 
     load.execute(); 
     Bitmap bitmap = load.get(); 
     update.setImageViewBitmap(R.id.imageView0, bitmap); 
+0

確保圖像位於該網址上。 – Subburaj

回答

1

您應該重寫onPostExecute在LoadImages類的方法,並設置位圖到您的ImageView在那個方法。當LoadImages任務完成時,它將調用onPostExcute方法。考慮WeakReference包裝你的ImageView以獲得更好的性能。