How to set image as wallpaper from the ImageView?

删除回忆录丶 提交于 2019-12-05 05:56:22

问题


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???????


回答1:


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!




回答2:


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



回答3:


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" />


来源:https://stackoverflow.com/questions/15424157/how-to-set-image-as-wallpaper-from-the-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!