嘿,我正在尝试通过 URL 在动态壁纸中加载图像……这可能吗?如果是这样,你能告诉我为什么这段代码不起作用(日志 - “无法从以下位置加载位图:” + 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());
}
}
...
位图工具
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;
}