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

2

7

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.

Taproot answered 5/2, 2010 at 4:41 Comment(1)
Related: Android - how to set the wallpaper imageLenoralenore
R
11

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 .

Regulate answered 28/4, 2011 at 4:22 Comment(1)
whats the use of this line? wallpaperDrawable = wallpaperManager.getDrawable();Diplocardiac
A
3

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.

Advance answered 5/2, 2010 at 8:14 Comment(5)
This is my current code... InputStream is = getContentResolver().openInputStream(imageUri); bgImage = BitmapFactory.decodeStream(is); Context context = this.getBaseContext(); context.setWallpaper(bgImage);` Errors at bgImage (line 2 and 4) and getBaseContext() (line 3) also whats the difference between a URI and a URL? Short answer i got was "A URL is a URI but, a URI is not a URL"Taproot
ok i fixed the errors on lines 2 and 4, i didn't define the Bitmap bgImage. but still errors on getBaseContext()Taproot
You know you can pass an instance of Activity as Context right? No need to do a this.getBaseContext() you can pass instance of current activity or a context object if you have one["this" would be a valid context object]Advance
And yes there is difference between URI and URL..Advance
i am getting here --- SkImageDecoder::Factory returned null..any suggestion hereChristianize

© 2022 - 2024 — McMap. All rights reserved.