开发者

Loading a URL image for a Live WallPaper

Hey there- I'm attempting to load an image within a Live Wallpaper via a URL... is it possible? If so can you tell my why this code isn't working (Log - "Could not load Bitmap from: " + url)? Thanks!

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


Yes, it is possible. I do it all the time. :-)

Probably your "issue" is that you have neglected to put

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

in your AndroidManifest.xml

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜