Android Set Image As Wallpaper
Please show me an example code on how to set image as wallpaper using Android WallpaperManager. I have shortened and edited my question. Hopefully you guys could understand my ques
Solution 1:
Try below code in ImagePagerActivity, i tested below code and it is working.
// fetch bitmap from viewpublicstatic Bitmap getBitmapFromView(View view) {
BitmapreturnedBitmap= Bitmap.createBitmap(view.getWidth(), view
.getHeight(), Bitmap.Config.ARGB_8888);
Canvascanvas=newCanvas(returnedBitmap);
DrawablebgDrawable= view.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else// if we unable to get background drawable then we will set white color as wallpaper
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}
publicvoidsetWall(int i) {
WallpaperManagermyWallpaperManager= WallpaperManager.getInstance(getApplicationContext());
try {
// below line of code will set your current visible pager item to wallpaper// first we have to fetch bitmap from visible view and then we can pass it to wallpaper
myWallpaperManager.setBitmap(getBitmapFromView(pager.getChildAt(1)));
// below line of code will set input stream data directly to wallpaper// myWallpaperManager.setStream(InputStream Data);// below line of code will set any image which is in the drawable folder // myWallpaperManager.setResource(R.drawable.icon);
} catch (IOException e) {
e.printStackTrace();
}
}
It will set current visible pager's item view(if it is progress wheel or image).
Solution 2:
This might help you with your setbackground method...
StringimagePath=""; // YOUR PATH HEREFileInputStreamis=newFileInputStream(newFile(imagePath));
BufferedInputStreambis=newBufferedInputStream(is);
Bitmapb= BitmapFactory.decodeStream(bis);
BitmapbitmapToUse= Bitmap.createScaledBitmap(b, parent.getWidth(), parent.getHeight(), true);
b.recycle();
if(!("").equals(imagePath)){
WallpaperManagerwallpaperManager= WallpaperManager.getInstance(this);
DrawablewallpaperDrawable= wallpaperManager.getDrawable();
wallpaperManager.setBitmap(bitmapToUse);
}
That should set your file to your wallpaper no problem.
Can you be more specific on what you need for the "saveImage()"? Where are you trying to save from? Is it local storage? Or a website? More details please.
[Edit] Updated code for clarity
[Edit 2] To save images from a URL...
FileimageFile=newFile("image.png"); // This is location AND file name, all i put here was the filenameURLurl=newURL("http://www.whatever.com/image.png");
Bitmapbmp= BitmapFactory.decodeStream(url.openConnection()
.getInputStream());
FileOutputStreamout=newFileOutputStream(imageFile);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
[Edit 3]
The 'parent' is either your parent view (generally the view for the current activity). There are other ways to set this, the parent.width/height is how you're going to define how large the wallpaper image needs to be.
Post a Comment for "Android Set Image As Wallpaper"