2011-05-27 29 views
0

嘿那裏,我試圖通過一個URL在動態壁紙中加載圖像...這可能嗎?如果是這樣,你可以告訴我爲什麼這段代碼不工作(日誌 - 「無法加載位圖:」+ url)?謝謝!加載一個Live WallPaper的URL圖像

引擎 - 可運行 - 運行():

... 
c = holder.lockCanvas(); 
if (c != null) { 
    try { 
    final Bitmap b = BitmapUtils.loadBitmap("http://mw2.google.com/mw-panoramio/photos/medium/17287086.jpg"); 
    c.drawBitmap(b, 0, 0, null);                
    } catch (Exception e) { 
    Log.e("Debug", e.toString()); 
    } 
} 
... 

BitmapUtils

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

     try { 
      in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE); 

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

      final byte[] data = dataStream.toByteArray(); 
      bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); 
     } catch (IOException e) { 
      Log.e(TAG, "Could not load Bitmap from: " + url); 
     } finally { 
      closeStream(in); 
      closeStream(out); 
     } 

     return bitmap; 
    } 
+0

如果你檢查你的logcat你應該看到它告訴你一個信息或警告日誌在您的代碼中發生IOException。 – Torid 2011-05-27 01:11:28

回答

1

是的,這是可能的。我一直這樣做。 :-)

也許你的「問題」是,你忽略了把

<uses-permission android:name="android.permission.INTERNET" /> 

在AndroidManifest.xml中

+0

謝謝喬治! – worked 2011-05-27 13:20:09