Search code examples
androidandroid-intentandroid-wallpaperandroid-glide

How to set Wallpaper without WallpaperManager


I am using Glide to cache Images in an ImageView. Now I want to set the image in the ImageView as the wallpaper of the phone without using the WallpaperManager, because the WallpaperManager does not give the option to crop/pan the Image like the wallpaper setting of default gallery app does.

I want to set Wallpaper using:

Intent.ACTION_ATTACH_DATA

or

Intent.ACTION_SET_WALLPAPER

but I don't know how to pass the Bitmap/drawable from the ImageView to that intent. Here's my code:

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"));

When I execute this code, I get a pull-up menu with installed galleries to select wallpaper. And when I choose one of them i get a Toast-

"Cannot load the image."

which is probably because I'm not passing any image to the Intent. Please Help, I've been searching for this for hours.

I found Similar questions here, here & here, but none of them worked for me.


Solution

  • So, as I've solved the problem, now I'm writing the solution here(in case anybody else needs it).

        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"));
    

    where, imageFile is the File that is needed to be set as wallpaper.