android 设置wallpaper

JAVA学习网 2017-08-21 18:54:01

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中需要申明权限:

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

阅读(762) 评论(0)