-
-
public static byte[] getBytes(InputStream is) throws IOException {
-
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
-
byte[] buffer = new byte[1024];
-
int len = -1;
-
while ((len = is.read(buffer)) != -1) {
-
outstream.write(buffer, 0, len);
-
}
-
outstream.close();
-
-
return outstream.toByteArray();
-
}
-
-
-
byte[] data = getBytes(new URL(imgUrl).openStream());
-
Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
以下是一個設置壁紙的實例應用
new Thread(){
@Override
public void run(){
//你要執行的方法
try {
byte[] data = getBytes(new URL(picurl).openStream());
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
wallpaperManager.setBitmap(bitmap);
}
catch (Exception e) {
Toast.makeText(DialogPhotoEntry.this,"設置桌面圖片失敗", 1000).show();
e.printStackTrace();
}
//執行完畢後給handler發送一個空消息
handler.sendEmptyMessage(0);
}
}.start();