加载Live WallPaper的URL图像

时间:2011-05-27 00:46:35

标签: android live-wallpaper

嘿那里 - 我试图通过URL在动态壁纸中加载图像......是否可能?如果是这样,你能告诉我为什么这段代码不起作用(日志 - “无法加载位图:”+ url)?谢谢!

引擎 - Runnable - run():

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

1 个答案:

答案 0 :(得分:1)

是的,有可能。我一直这样做。 : - )

可能你的“问题”是你忽略了

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

在你的AndroidManifest.xml