如何在没有 WallpaperManager 的情况下设置墙纸

How to set Wallpaper without WallpaperManager

我正在使用 Glide 在 ImageView 中缓存图像。 现在我想在不使用 WallpaperManager 的情况下将 ImageView 中的图像设置为 phone 的墙纸,因为 WallpaperManager 没有像默认图库应用程序的墙纸设置那样提供 crop/pan 图像的选项.

我想设置壁纸使用:

Intent.ACTION_ATTACH_DATA

Intent.ACTION_SET_WALLPAPER

但我不知道如何将 Bitmap/drawable 从 ImageView 传递到该意图。 这是我的代码:

Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
            intent.setDataAndType(ContactsContract.Contacts.CONTENT_URI, "image/*");
            intent.putExtra("mimeType", "image/jpeg");
            intent.putExtra("image", imageView.getDrawingCache());
            startActivity(Intent.createChooser(intent, "Select Wallpaper"));

当我执行此代码时,我得到一个上拉菜单,其中包含安装到 select 墙纸的图库。 当我选择其中之一时,我会得到一个祝酒词-

"Cannot load the image."

这可能是因为我没有将任何图像传递给 Intent。 请帮忙,我已经搜索了好几个小时了。

我发现了类似的问题 here, here & here,但其中 none 对我有用。

所以,我已经解决了这个问题,现在我将解决方案写在这里(以防其他人需要它)。

    myDir = new File(root + "/Wallpy/");     //this is the name of the containing folder
    final File imageFile = new File(myDir, "abcd.jpg");     //name of the image file
    Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
                    intent.setDataAndType(Uri.fromFile(imageFile), "image/*");
                    startActivity(Intent.createChooser(intent, "Select Wallpaper"));

其中,imageFile是需要设置为墙纸的文件