我是android新手,正在制作android壁纸应用。我将图像存储在谷歌驱动器上,并将链接保存在资产文件夹中的json文件中。问题是当我按下墙纸按钮时,墙纸没有改变,但是当我关闭/打开手机时它改变了。我不知道发生了什么。我搜索了更多但没有找到任何解决方案。请任何人帮助我。
WallpaperManager wallpaperManager ;
Bitmap bitmap1, bitmap2;
DisplayMetrics displayMetrics ;
int width, height;
BitmapDrawable bitmapDrawable ;
applyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wallpaperManager = WallpaperManager.getInstance(activity);
bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
bitmap1 = bitmapDrawable.getBitmap();
GetScreenWidthHeight();
SetBitmapSize();
wallpaperManager = WallpaperManager.getInstance(activity);
try {
wallpaperManager.setBitmap(bitmap2);
wallpaperManager.suggestDesiredDimensions(width, height);
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(activity,"Image set as Wallpaper",Toast.LENGTH_SHORT).show();
}
});
public void GetScreenWidthHeight(){
displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
width = displayMetrics.widthPixels;
height = displayMetrics.heightPixels;
}
public void SetBitmapSize(){
bitmap2 = Bitmap.createScaledBitmap(bitmap1, width, height, false);
}
答案 0 :(得分:0)
使用Picasso从网址加载图片并将其设置为墙纸:
在 .gradle 中:
implementation 'com.squareup.picasso:picasso:2.71828'
在您的 setOnClickListener 中:
Picasso.with(this) .load(imageUrl).centerCrop() .into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
wallpaperManager.setBitmap(bitmap)
}