gpt4 book ai didi

android - 如何使用代码在 android 中设置墙纸?

转载 作者:太空宇宙 更新时间:2023-11-03 11:27:01 26 4
gpt4 key购买 nike

我正在开发一个显示来自服务器的不同照片的应用程序,用户可以将选定的照片设置为其设备的墙纸我使用给定的代码设置墙纸它可以工作但图像设置不正确它不适合屏幕。我使用了这段代码。

String dirPath = getFilesDir().toString();
String folder = mPhotos.get(nextPosition - 1).getCategory();
String filePath = dirPath + "/PhotoViewer/" + folder + "/"
+ mPhotos.get(nextPosition - 1).getFileName();
File imageFile = new File(filePath);
Bitmap bitmap = BitmapFactory.decodeFile(imageFile
.getAbsolutePath());
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(getApplicationContext());
try
{
myWallpaperManager.setBitmap(bitmap);
Toast.makeText(PhotoActivity.this, "Wallpaper set",
Toast.LENGTH_SHORT).show();
} catch(IOException e){
Toast.makeText(PhotoActivity.this, "Error setting wallpaper",
Toast.LENGTH_SHORT).show();
}

最佳答案

要在 android 中设置壁纸,请使用以下代码:通过使用 WallpaperManager 类

Button buttonSetWallpaper = (Button)findViewById(R.id.set);
ImageView imagePreview = (ImageView)findViewById(R.id.preview);
imagePreview.setImageResource(R.drawable.five);

buttonSetWallpaper.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(R.drawable.five);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});

需要在Manifest中设置权限:

<uses-permission android:name="android.permission.SET_WALLPAPER"/>

关于android - 如何使用代码在 android 中设置墙纸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15892909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com