Android - How to set the wallpaper image? [duplicate]

孤人 提交于 2019-11-27 04:22:32

问题


Possible Duplicate:
Android - how to set the wallpaper image

What i'm trying to do is, set the wallpaper using an image URI (no cropping)

I'm a noob at dev on Android and dev in general. The internet has failed me... on providing code to set the wallpaper.

yes the dev resource site says

public void setStream (InputStream data)

but i don't understand it, some sample code would greatly help me.


回答1:


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

wallpaperManager = WallpaperManager.getInstance(this);
wallpaperDrawable = wallpaperManager.getDrawable();
mImageView.setImageURI(imagepath);

Let me know if there is any issue .




回答2:


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.

If you dont want to specify a stream directly, you can open the remote stream, create a Bitmap and then either use a WallpaperManager instance or do a context.setWallpaper(bitmap)(this is deprecated) to set your bitmap as the wallpaper.

For reference take a look at this thread.



来源:https://stackoverflow.com/questions/2205092/android-how-to-set-the-wallpaper-image

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