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
精彩评论