How to set image as wallpaper from the ImageView?

笑着哭i 提交于 2019-12-03 20:48:41
Shajeel Afzal

Step 1: Get the image attached to the ImageView.

Setp 2: Set that image as Wallpaper.

Step 3: Add permission in the AndroidManifest.xml to set wallpaper!

For step 1 check This answer!

imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();

For step 2:

WallpaperManager m=WallpaperManager.getInstance(this);

try {
    m.setBitmap(bmap);
} catch (IOException e) {
    e.printStackTrace();
}

For step 3: Include this permission too.

<uses-permission android:name="android.permission.SET_WALLPAPER" />

Tell me if that does not works for you!

This can be answered in a two parts.

The first would be to set the WallPaper:

WallpaperManager wallManager = WallpaperManager.getInstance(getApplicationContext());
try {
    wallManager.setBitmap(bmpImg);
    Toast.makeText(MainActivity.this, "Wallpaper Set Successfully!!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
    Toast.makeText(MainActivity.this, "Setting WallPaper Failed!!", Toast.LENGTH_SHORT).show();
}

The second part is kinda optional and would come in to picture only if you haven't set a Bitmap to your ImageView. In that case, you will need to do this step before setting up the WallPaper:

Bitmap bmpImg = ((BitmapDrawable)yourImageView.getDrawable()).getBitmap();

for set wallpaper:

            Bitmap bitmapImg = ((BitmapDrawable) YourImageView.getDrawable()).getBitmap();

            WallpaperManager wallManager = WallpaperManager.getInstance(getApplicationContext());
            try {
                wallManager.clear();
                wallManager.setBitmap(bitmapImg);


            } catch (IOException ex) {

            }

You have to add two permissions in manifest file

1. <uses-permission android:name="android.permission.SET_WALLPAPER" />
2. <uses-permission android:name="android.permission.WRITE_SETTINGS" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!