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