Android - 如何设置壁纸图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2205092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - How to set the wallpaper image?
提问by asdf.BEN
Possible Duplicate:
Android - how to set the wallpaper image
可能重复:
Android - 如何设置壁纸图像
What i'm trying to do is, set the wallpaper using an image URI (no cropping)
我想要做的是,使用图像 URI 设置壁纸(无裁剪)
I'm a noob at dev on Android and dev in general. The internet has failed me... on providing code to set the wallpaper.
我是 Android 开发人员和一般开发人员的菜鸟。互联网让我失败了……提供设置壁纸的代码。
yesthe dev resource site says
是的,开发资源网站说
public void setStream (InputStream data)
public void setStream (InputStream data)
but i don't understand it, some sample code would greatly help me.
但我不明白,一些示例代码会对我有很大帮助。
回答by Maidul
Hi you can use this code if You have Image path.
嗨,如果您有图像路径,则可以使用此代码。
is = new FileInputStream(new File(imagePath));
bis = new BufferedInputStream(is);
Bitmap bitmap = BitmapFactory.decodeStream(bis);
Bitmap useThisBitmap = Bitmap.createScaledBitmap(
bitmap, parent.getWidth(), parent.getHeight(), true);
bitmap.recycle();
if(imagePath!=null){
System.out.println("Hi I am try to open Bit map");
wallpaperManager = WallpaperManager.getInstance(this);
wallpaperDrawable = wallpaperManager.getDrawable();
wallpaperManager.setBitmap(useThisBitmap);
if you have image URI then use this
如果您有图像 URI,则使用它
wallpaperManager = WallpaperManager.getInstance(this);
wallpaperDrawable = wallpaperManager.getDrawable();
mImageView.setImageURI(imagepath);
Let me know if there is any issue .
让我知道是否有任何问题。
回答by Samuh
If you have the image URL you can open the resource it represent using the stream(abstraction):
new URL("your.image.url.com").openStream()
. This method call will return an object of type InputStream
which you can pass as an argument to setStream()
method.
如果您有图像 URL,则可以使用 stream(abstraction): 打开它代表的资源
new URL("your.image.url.com").openStream()
。此方法调用将返回一个类型的对象InputStream
,您可以将其作为参数传递给setStream()
方法。
If you dont want to specify a stream directly, you can open the remote stream, create a Bitmap and then either use a WallpaperManagerinstance or do a context.setWallpaper(bitmap)
(this is deprecated) to set your bitmap as the wallpaper.
如果不想直接指定流,可以打开远程流,创建位图,然后使用WallpaperManager实例或执行context.setWallpaper(bitmap)
(已弃用)将位图设置为墙纸。
For reference take a look at thisthread.
作为参考,请查看此线程。