Home screen wallpaper gets zoomed when it goes to the background - android

I have scheduled background service to change device home screen wallpaper periodically. The wallpaper changes successfully and they are perfectly set to the screen size.
But when I move away from the home screen and return the image gets zoomed in too much,leaving only a portion of the image visible.Below given is the code I use
Bitmap theBitmap = null;
try {
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
theBitmap = Glide.with(mContext)
.load(PhotoPath)
.asBitmap().into(width, height).get();
WallpaperManager wm = WallpaperManager.getInstance(mContext);
wm.setWallpaperOffsetSteps(1, 1);
wm.suggestDesiredDimensions(width, height);
try {
wm.setBitmap(theBitmap);Album
} catch (IOException e) {
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
Try not to rush and flag this question as a duplicate. There are questions stating the image appears zoomed in when set but My wallpaper gets set perfectly,atleast initially it does. Why does it zoom in when I move homescreen to the background.

Related

Image dimensions obtained from bitmapOptions.outWidth and bitmapOptions.outHeight are exchanged

I am trying to obtain the dimensions of an image using its URI using the code below. However, for some images (photos taken by phone camera in the portrait mode - an example photo below) the obtained width and height are exchanged (bitmapOptions.outWidth = real height) and (bitmapOptions.outHeight = real width).
By real width/real height, I mean the dimensions which can be seen when opening the image in a viewer or when looking at image details using file browser of my phone or a PC.
I didn't find a solution for this anywhere here. Could anyone tell me what is a robust method to obtain always the real width and height of an image using its URI, and why this is happening?!
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = true;
InputStream inputStream = null;
try {
inputStream = context.getContentResolver().openInputStream(imageUri);
BitmapFactory.decodeStream(inputStream, null, bitmapOptions);
} catch (FileNotFoundException e) {
// do something
}
int imageWidth = bitmapOptions.outWidth;
int imageHeight = bitmapOptions.outHeight;
Example photo here.
// It will give you the height and width
if (Build.VERSION.SDK_INT >= 29) {
try {
bitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(getContentResolver(), ImageUri));
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Log.d(TAG,"Height is : "+h+"\n Width is : "+w);
} catch (IOException e) {
e.printStackTrace();
}
} else {
// Use older version
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), ImageUri);
} catch (IOException e) {
e.printStackTrace();
}
}

Image gets zoomed while setting as wallpaper in my android application

I am trying to set my image as wallpaper but it gets zoomed/cropped when set as wallpaper. I have tried almost everything but nothing works.
Here is my piece of code:
public void setAswallpaper(Bitmap wallpaper)
{
WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
System.out.println("screen dimensions "+width+" "+height);
System.out.println("wallpaper dimensions "+wallpaper.getWidth()+" "+wallpaper.getHeight());
wallpaper = Bitmap.createScaledBitmap(wallpaper, width*2 , height, true);
try {
WallpaperManager wallpaperManager=WallpaperManager.getInstance(context);
wallpaperManager.suggestDesiredDimensions(width,height);
System.out.println("wallpaper manager dimensions "+wallpaperManager.getDesiredMinimumWidth()+" "+wallpaperManager.getDesiredMinimumHeight());
wallpaperManager.setBitmap(wallpaper); }
}
catch (IOException e)
{
e.printStackTrace();
Toast.makeText(context,
"Wallpaper Not Set",
Toast.LENGTH_SHORT).show();}
}
I need help so that I can set my complete image as wallpaper
My image size is : 640*960
bitmap size is : 1960*2880
screen dimensions : 1080*1960

Android get wallpaper name

I am developing an Android app for our company which sets the wallpaper to a specific company wallpaper every time the phone is booted. It would be preferable to check to see if the wallpaper has been changed rather than running the code to change the wallpaper.
Is there any way to get identifying information (e.g. filename etc.) from the current wallpaper?
WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
WallpaperInfo wallpaperInfo = wallpaperManager.getWallpaperInfo();
returns null for wallpaperInfo.
Code for wallpaper change:
public static void setWallpaper(Context context) {
// Has wallpaper changed?
if (/*--wallpaperNotChanged--*/) {
return;
}
try {
// Setup
Drawable drawable = context.getResources().getDrawable(R.drawable.test);
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
// Get display sizes
windowManager.getDefaultDisplay().getMetrics(displayMetrics);
// Create Bitmap
Bitmap unscaledWallpaper = BitmapFactory.decodeResource(context.getResources(), R.drawable.test);
Bitmap wallpaper = Bitmap.createScaledBitmap(unscaledWallpaper, displayMetrics.widthPixels, displayMetrics.heightPixels, true);
// Set wallpaper
wallpaperManager.setBitmap(wallpaper);
} catch (Exception e){
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
Please read this get-current-wallpaper. And Get current wallpaper absolute path
final Drawable wallpaperManager = wallpaperManager.getDrawable();
For better info you can visit:
https://developer.android.com/reference/android/app/WallpaperInfo.html
WallpaperManager API Reference
public WallpaperInfo getWallpaperInfo ()
Here it says:
If the current wallpaper is a live wallpaper component, return the information about that wallpaper. Otherwise, if it is a
static image, simply return null.
You must be having a static image wallpaper in your background, not a live one.

Android Images are stretched when the phone is rotated

I am getting images using the Html.fromHtml() method. So I am using URLImageParser and URLDrawable classes as defined here: Android HTML ImageGetter as AsyncTask.
I have adapted two methods from URLImageParser in order to scale the image to the width of the screen whilst maintaining the aspect ratio. The changed methods are as follows:
#Override
protected void onPostExecute(Drawable result) {
DisplayMetrics dm = new DisplayMetrics();
((WindowManager) c.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = width * container.getHeight() / container.getWidth();
urlDrawable.setBounds(0, 0, 0+width, 0+height);
// change the reference of the current drawable to the result
// from the HTTP call
urlDrawable.drawable = result;
// redraw the image by invalidating the container
URLImageParser.this.container.invalidate();
// For ICS
URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight()
+ height));
// Pre ICS
URLImageParser.this.container.setEllipsize(null);
}
and
public Drawable fetchDrawable(String urlString) {
try {
InputStream is = fetch(urlString);
Drawable drawable = Drawable.createFromStream(is, "src");
DisplayMetrics dm = new DisplayMetrics();
((WindowManager) c.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = width * container.getHeight() / container.getWidth();
drawable.setBounds(0, 0, 0 + width, 0
+ height);
return drawable;
} catch (Exception e) {
return null;
}
}
My problem is that when the phone is held in portrait orientation the image looks fine. However when I rotate the phone the width of the image goes to the new width of the phone, however the height does not change so the image looks distorted.
Is there a way to reset the height when the phone is rotated?
Pretty sure this is a silly mistake somewhere
To detect a screen rotation, you can try something like this:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
See more.
Here is another great example.

Programatically set background (drawable) doesn't center

I wrote an app that lets people select an image (drawable) and after they hit the "set as background" button the selected image should appear centered as the background of my phone.
Following code used to work fine. The image was placed and centered. But for operating systems (> api 13) it is depreciated. Does anyone know how to fix so that the image is also centered (or scaled to fit the screen if you wish)?
I've tried replacing it with "displaymetrics" or "points" to get w or h but it doesn't help centering the image. Is it the "suggestDesiredDimensions" that doesn't do it anymore?
ResID is the identifier of the needed drawable.
Anyone knows how to handle this? Thanks!
public void setBackground(View v)
{
try {
// Set background from a resource
WallpaperManager.getInstance(this).setResource(resID);
WallpaperManager wm = (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
Display display = getWindowManager().getDefaultDisplay();
int w = display.getWidth(); // deprecated
int h = display.getHeight(); //deprecated
wm.suggestDesiredDimensions(w, h);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Try using display.getSize()
public void setBackground(View v){
try {
// Set background from a resource
WallpaperManager.getInstance(this).setResource(resID);
WallpaperManager wm = (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int w = size.x;
int h = size.y;
wm.suggestDesiredDimensions(w, h);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Categories

Resources