How to set image as wallpaper from the ImageView?

2020-06-28 06:32发布

I have an activity in which there are two Buttons and an ImageView. One button is to take image from the Camera application of the phone and set it to the ImageView, and other Button is to set that image as the Home screen wallpaper so i want the code how to set this image from the ImageView to the wallpaper???????

3条回答
贼婆χ
2楼-- · 2020-06-28 07:24

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!

查看更多
够拽才男人
3楼-- · 2020-06-28 07:25

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();
查看更多
小情绪 Triste *
4楼-- · 2020-06-28 07:25

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" />
查看更多
登录 后发表回答