我获取当前壁纸并将其转换为位图并再次将其转换为字符串base64并将其保存在sharedprefrences中并从shareprefrences获取壁纸并将其设置为壁纸,所有代码都在循环中,每5秒设置一次。在那里工作的每件事只有一个问题wallapeper设置正确,但是当它再次设置壁纸崩溃或缓慢破坏后再次设置。请看我的代码为什么壁纸慢慢慢慢崩溃。请帮我一些代码谢谢。
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
Drawable wallpaper_profile3 = wallpaperManager.getDrawable();
Bitmap bitmap_profile3 = ((BitmapDrawable) wallpaper_profile3).getBitmap();
ByteArrayOutputStream baos_profile3 = new ByteArrayOutputStream();
bitmap_profile3.compress(Bitmap.CompressFormat.JPEG, 100, baos_profile3);
final byte[][] b_profile3 = {baos_profile3.toByteArray()};
String encodedImage_profile3 = Base64.encodeToString(b_profile3[0], Base64.DEFAULT);
wallpaper_sharedprefrences_profile3 = context.getSharedPreferences(PREF_PROFILE3, Context.MODE_PRIVATE);
SharedPreferences.Editor edit = wallpaper_sharedprefrences_profile3.edit();
edit.putString(PROFILE2_WALLPAPER, encodedImage_profile3);
edit.commit();
load_wallpaper_profile3 = wallpaper_sharedprefrences_profile3.getString(PROFILE2_WALLPAPER, "");
ByteArrayOutputStream baos2_profile3 = new ByteArrayOutputStream();
final byte[][] b2_profile3 = {baos2_profile3.toByteArray()};
b2_profile3[0] = Base64.decode(load_wallpaper_profile3 , Base64.DEFAULT);
Bitmap bit_profile3 = BitmapFactory.decodeByteArray(b2_profile3[0], 0, b2_profile3[0].length);
WallpaperManager wallpaper = WallpaperManager.getInstance(context);
try {
wallpaper.setBitmap(bit_profile3);
} catch (IOException e) {
e.printStackTrace();
}
// wallpaper.suggestDesiredDimensions(width, height);
Toast.makeText(context, "Wallpaper Set", Toast.LENGTH_SHORT).show();
handler.postDelayed(this, 5000);
}
}, 5000);
这是真实的形象