'WallpaperManager intent for crop and resize from bitmap

Right now we are setting wallpaper from some loaded images using below code.

bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
     myWallpaperManager.setBitmap(bitmap);
} catch (IOException e) {                                
  e.printStackTrace();
}
Toast.makeText(this, R.string.set_wallpaper_complete_toast,Toast.LENGTH_SHORT).show();
return true;

How can we use the getCropAndSetWallpaperIntent(Uri imageUri) in the above code, so that it will ask option to set wallpaper and crop it.



Solution 1:[1]

    public Uri getImageUri(Context inContext, Bitmap inImage) {
      ByteArrayOutputStream bytes = new ByteArrayOutputStream();
      inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
      String path = Images.Media.insertImage(inContext.getContentResolver(), 
inImage, "Title", null);
      return Uri.parse(path);
    } 

Then,

bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
WallpaperManager myWallpaperManager
              = WallpaperManager.getInstance(getApplicationContext());
                try { 
                   Uri myImageUri = getImageUri(bitmap);
                    Intent intent = new Intent(myWallpaperManager.getCropAndSetWallpaperIntent(myImageUri));
                    startActivity(intent);
                } catch (IOException e) {
                    // TODO Auto-generated catch block 
                    e.printStackTrace();
                    return false;
                } 
                Toast.makeText(
                        this, R.string.set_wallpaper_complete_toast,Toast.LENGTH_SHORT).show();
                return true; 

Solution 2:[2]

Use following

val path: String = MediaStore.Images.Media.insertImage(contentResolver, bitmap, "wallpaper.jpg", null)
        val intent = Intent(wallpaperManager.getCropAndSetWallpaperIntent(Uri.parse(path)))
        startActivity(intent)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Muhammad Faizan
Solution 2 Muhammad Faizan