如何使用户手动设置墙纸?

时间:2019-10-31 11:31:23

标签: android

我确实设置了墙纸功能,但不是手动设置 set wallpaper

代码:

private void setWallpaper()
{
    Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
    WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
    try {
        manager.setBitmap(bitmap);
        Toast.makeText(ViewWallpaperActivity.this,"Done!",LENGTH_LONG).show();
    } catch (IOException e) {
        Toast.makeText(ViewWallpaperActivity.this,"Error!",LENGTH_LONG).show();
    }
}

1 个答案:

答案 0 :(得分:1)

尝试使用“设置墙纸”表的代码:

private void setWallpaper(){
    Uri uri = getImageUri(getApplicationContext(), bitmap);
    Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    intent.setDataAndType(uri, "image/jpeg");
    intent.putExtra("mimeType", "image/jpeg");
    startActivity(Intent.createChooser(intent, "Set as:"));
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}