【发布时间】:2013-03-15 03:26:39
【问题描述】:
我有一个活动,其中有两个按钮和一个 ImageView。一个按钮是从手机的相机应用程序中获取图像并将其设置为 ImageView,另一个按钮是将该图像设置为主屏幕壁纸,所以我想要代码如何将此图像从 ImageView 设置为壁纸? ??????
【问题讨论】:
标签: android imageview wallpaper
我有一个活动,其中有两个按钮和一个 ImageView。一个按钮是从手机的相机应用程序中获取图像并将其设置为 ImageView,另一个按钮是将该图像设置为主屏幕壁纸,所以我想要代码如何将此图像从 ImageView 设置为壁纸? ??????
【问题讨论】:
标签: android imageview wallpaper
第 1 步: 获取附加到 ImageView 的图像。
Setp 2:将该图像设置为墙纸。
第 3 步:在AndroidManifest.xml 中添加设置壁纸的权限!
第 1 步检查This 答案!
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
对于第 2 步:
WallpaperManager m=WallpaperManager.getInstance(this);
try {
m.setBitmap(bmap);
} catch (IOException e) {
e.printStackTrace();
}
对于第 3 步: 也包括此权限。
<uses-permission android:name="android.permission.SET_WALLPAPER" />
如果这不适合你,请告诉我!
【讨论】:
这可以分为两部分来回答。
首先是设置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();
}
第二部分是可选的,只有在您没有为ImageView 设置位图时才会显示出来。 在这种情况下,您需要在设置WallPaper之前执行此步骤:
Bitmap bmpImg = ((BitmapDrawable)yourImageView.getDrawable()).getBitmap();
【讨论】:
设置壁纸:
Bitmap bitmapImg = ((BitmapDrawable) YourImageView.getDrawable()).getBitmap();
WallpaperManager wallManager = WallpaperManager.getInstance(getApplicationContext());
try {
wallManager.clear();
wallManager.setBitmap(bitmapImg);
} catch (IOException ex) {
}
你必须在清单文件中添加两个权限
1. <uses-permission android:name="android.permission.SET_WALLPAPER" />
2. <uses-permission android:name="android.permission.WRITE_SETTINGS" />
【讨论】: