加载动态壁纸的 URL 图像

发布于 2024-11-10 03:53:13 字数 1360 浏览 3 评论 0原文

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

引擎 - 可运行 - 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;
    }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮筵 2024-11-17 03:53:13

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

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

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

AndroidManifest.xml

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文