[android studio]android 设置wallpaper的操作方法

时间:2021-08-11  来源:Action  阅读:

1、调用系统的Intent.ACTION_ATTACH_DATA,该Intent会唤起所有的设置壁纸程序以及设置联系人头像程序,用户可以通过ChooseActivity进行选择:

该Intent是一个标准Intent,因此所有设置都会支持

Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
       intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
       intent.putExtra("mimeType", "image/*");
       Uri uri = Uri.parse(MediaStore.Images.Media
           .insertImage(getActivity().getContentResolver(),
               ((BitmapDrawable) wallpaper).getBitmap(), null, null));
       intent.setData(uri);
       startActivityForResult(intent, SET_WALLPAPER);

2、通过WallpaperManager设置

该方法可以直接将图片置为壁纸,对于所有平台的Android系统都使用,但无法裁剪/调整图片。

try {
  WallpaperManager wpm = (WallpaperManager) getActivity().getSystemService(
        Context.WALLPAPER_SERVICE);
 
  if (wallpaper != null) {
    wpm.setBitmap(bitmap);
    Log.i("xzy", "wallpaper not null");
  }
} catch (IOException e) {
  Log.e(TAG, "Failed to set wallpaper: " + e);
}

AndroidManifest.xml中需要申明权限:

[android studio]android 设置wallpaper的操作方法

http://m.bbyears.com/flash/135315.html

推荐访问:android学习路线
相关阅读 猜你喜欢
本类排行 本类最新