Android - 如何设置壁纸图片

Jam*_*add 27 android image wallpaper

是否有可能以编程方式设置android壁纸图片?我想创建一个从Web下载图像并定期更新主屏幕壁纸的服务.

Kis*_*ore 32

如果您有图片网址,请使用

WallpaperManager wpm = WallpaperManager.getInstance(context);
InputStream ins = new URL("absolute/path/of/image").openStream();
wpm.setStream(ins);
Run Code Online (Sandbox Code Playgroud)

如果你有图像URI,那么使用

WallpaperManager wpm = WallpaperManager.getInstance(context);
wpm.setResource(Uri.of.image);
Run Code Online (Sandbox Code Playgroud)

在您的清单文件中:

<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
Run Code Online (Sandbox Code Playgroud)


Chr*_*isF 22

从开发者网站上的此页面:

public void setStream (InputStream data)
Run Code Online (Sandbox Code Playgroud)

将当前系统壁纸更改为特定字节流.将给出的InputStream复制到持久存储中,现在将用作壁纸.目前它必须是JPEG或PNG图像.

  • 注意,API首先出现在2.0中; 如果您想支持旧版本,请在Context上使用原始API之一:http://developer.android.com/reference/android/content/Context.html#setWallpaper(java.io.InputStream) (8认同)

djk*_*djk 5

如果你有图像的位图,你将添加此功能设置为壁纸:

  public void SetBackground(int Url) {

    try {
        File file = new File("/sdcard/sampleimage");
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), Url);
        bitmap.compress(CompressFormat.JPEG, 80, new FileOutputStream(file));
        Context context = this.getBaseContext();
        context.setWallpaper(bitmap);            
        Toast.makeText(getApplicationContext(), "Wallpaper has been set",             Toast.LENGTH_SHORT).show();            
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }         
}
Run Code Online (Sandbox Code Playgroud)

你应该为此添加权限

<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
Run Code Online (Sandbox Code Playgroud)

希望它会奏效