设置为墙纸

时间:2016-06-22 09:26:15

标签: android

我可以更改设备的壁纸,但是我希望用户在点击设置壁纸按钮时选择gibe选项,即要么将图像设置为主屏幕,要么设置为whatsapp或用于facebook。

这是我设置壁纸的工作方法:

    fullImageView = (ImageView) findViewById(R.id.imgFullscreen);

public void setAsWallpaper(Bitmap bitmap) {
        try {
            WallpaperManager wm = WallpaperManager.getInstance(_context);

            wm.setBitmap(bitmap);
            Toast.makeText(_context,
                    _context.getString(R.string.toast_wallpaper_set),
                    Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(_context,
                    _context.getString(R.string.toast_wallpaper_set_failed),
                    Toast.LENGTH_SHORT).show();
        }
    }

和点击方法代码:

public void onClick(View v) {
        Bitmap bitmap = ((BitmapDrawable) fullImageView.getDrawable())
                .getBitmap();

        switch (v.getId()) {
        // button Download Wallpaper tapped
        case R.id.llDownloadWallpaper:
            utils.saveImageToSDCard(bitmap);
            break;
        // button Set As Wallpaper tapped
        case R.id.llSetWallpaper:

            utils.setAsWallpaper(bitmap);
            break;
        default:
            break;
        }

    }

工作代码会非常有用。

1 个答案:

答案 0 :(得分:0)

private void setImageAsWallpaperPicker(Uri path) {
    Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
    intent.setType("image/*");
    MimeTypeMap map = MimeTypeMap.getSingleton();
    String mimeType = map.getMimeTypeFromExtension("png");
    intent.setDataAndType(path, mimeType);
    intent.putExtra("mimeType", mimeType);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(Intent.createChooser(intent, "Set as"));
}