Wallpaper not properly fit on device screen

I have one set of images in one drawable folder. I have one button to set image as wallpaper on device screen. But when I set this image as wallpaper its either zoom or cropped. I want image should fit on screen size. I have seen lots of links on SO but no link is work for me. This is the code I am trying so far.

Code-

  • Android - Dynamicly Get the Current Activity in Foreground
  • android listview first visible position
  • Android WebView not stopping after user presses back
  • Deep Link Fallback
  • Horizontal “tab”ish scroll between views
  • Android Holo Light styling changes depending on chosen context
  • Bitmap bmap2 = BitmapFactory.decodeStream(getResources().openRawResource(mThumb[position]));
    DisplayMetrics metrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int height = metrics.heightPixels; 
    int width = metrics.widthPixels;
    Bitmap bitmap = Bitmap.createScaledBitmap(bmap2, width, height, true); 
    WallpaperManager wallpaperManager = WallpaperManager.getInstance(this); 
    try {
      wallpaperManager.setBitmap(bitmap);
      } catch (IOException e) {
      e.printStackTrace();
    }
    

    I also added following lines in manifest-

    <uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>
    <uses-permission android:name="android.permission.SET_WALLPAPER" />
    

    Related posts:

    onSearchRequested() no longer works in Android 4.x+?
    getActivity() call causes RuntimeException: Could not launch intent Intent act=android.intent.action...
    Searchable activity being called twice
    Unsupported operation: Android, Retrofit, OkHttp. Adding interceptor in OkHttpClient
    Clone textview to append it to a ViewGroup
    List of useful Android libraries for android development and applications
  • Simple Android toasts not aligning properly
  • Cannot add multiple fragments to LinearLayout
  • What's the difference between alignBaseline and alignBottom in Android?
  • Running a specific unit test with gradle
  • Converting double to string
  • Intellij android project setup - run button is disabled
  • 5 Solutions collect form web for “Wallpaper not properly fit on device screen”

    Hello This is working with drawable image i have checked it..

                    DisplayMetrics metrics = new DisplayMetrics(); 
                    getWindowManager().getDefaultDisplay().getMetrics(metrics);
                    int height = metrics.heightPixels; 
                    int width = metrics.widthPixels;
                    Bitmap tempbitMap = BitmapFactory.decodeResource(getResources(), R.drawable.img);
                    Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap,width,height, true);
                    WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this); 
                    wallpaperManager.setWallpaperOffsetSteps(1, 1);
                    wallpaperManager.suggestDesiredDimensions(width, height);
                    try {
                      wallpaperManager.setBitmap(bitmap);
                      } catch (IOException e) {
                      e.printStackTrace();
                    }
    

    Also mention these permissions in Manifest.xml..

        <uses-permission android:name="android.permission.SET_WALLPAPER_HINTS" />
        <uses-permission android:name="android.permission.SET_WALLPAPER" />
    

    This is screenshot..

    enter image description here

    For reset Fit wallpaper of screen store the image path in shared preferences and use Boot Completed Receiver then reset the same wallpaper on the screen….

    The broadcast receiver is..

    import java.io.IOException;
    
    import android.app.WallpaperManager;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.util.DisplayMetrics;
    import android.util.Log;
    import android.view.WindowManager;
    
    public class BootReceiver extends BroadcastReceiver {
    private static final String TAG="BootReceiver";
    
    @Override public void onReceive(Context context,Intent intent){
        try{
                DisplayMetrics metrics = new DisplayMetrics(); 
                WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                windowManager.getDefaultDisplay().getMetrics(metrics);
                int height = metrics.heightPixels; 
                int width = metrics.widthPixels;
                Bitmap tempbitMap = BitmapFactory.decodeResource(context.getResources(), R.drawable.img);
                Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap,width,height, true);
                WallpaperManager wallpaperManager = WallpaperManager.getInstance(context); 
                wallpaperManager.setWallpaperOffsetSteps(1, 1);
                wallpaperManager.suggestDesiredDimensions(width, height);
                try {
                  wallpaperManager.setBitmap(bitmap);
                  } catch (IOException e) {
                  e.printStackTrace();
                }
        }catch(Exception e){
            Log.e(TAG,e.toString());
        }
    }
    }
    

    After Add these lines in Manifest.xml

           <receiver
                android:name=".BootReceiver"
                android:enabled="true"
                android:exported="true"
                android:label="BootReceiver" >
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" >
                    </action>
                </intent-filter>
            </receiver>
    

    try this code

    Bitmap bmap2 =BitmapFactory.decodeStream(getResources().openRawResource(mThumb[position]));
        DisplayMetrics metrics = new DisplayMetrics(); 
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int height = metrics.heightPixels; 
        int width = metrics.widthPixels;
        Bitmap bitmap = Bitmap.createScaledBitmap(bmap2, width, height, true); 
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(this); 
        wallpaperManager.setWallpaperOffsetSteps(1, 1);
        wallpaperManager.suggestDesiredDimensions(width, height);
        try {
          wallpaperManager.setBitmap(bitmap);
          } catch (IOException e) {
          e.printStackTrace();
        }
    

    If you have image URL then use :

    WallpaperManager wpm = WallpaperManager.getInstance(context);
    InputStream ins = new URL("absolute/path/of/image").openStream();
    wpm.setStream(ins);
    

    If you have image URI then use

    WallpaperManager wpm = WallpaperManager.getInstance(context);
    wpm.setResource(Uri.of.image);
    

    In your manifest file:

    <uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
    

    and don’t forget to place this code in AsyncTask.

    Try this code:

    public void changeWallpaper(String path) {
            FileInputStream is;
            BufferedInputStream bis;
            WallpaperManager wallpaperManager;
            Drawable wallpaperDrawable;
            File sdcard = Environment.getExternalStorageDirectory();
            try {
                is = new FileInputStream(new File(path));
                bis = new BufferedInputStream(is);
                Bitmap bitmap = BitmapFactory.decodeStream(bis);
                Bitmap useThisBitmap = Bitmap.createBitmap(bitmap);
                wallpaperManager = WallpaperManager.getInstance(getActivity());
                wallpaperDrawable = wallpaperManager.getDrawable();
                wallpaperManager.setBitmap(useThisBitmap);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

    In this example , I have used image from my device SD card…

    It’s perfectly worked for me..

    notice: maybe not work for you.

    you need this:

    WallpaperManager wallpaperManager = WallpaperManager.getInstance(this); 
                 try {
                  wallpaperManager.setBitmap(bitmap);
    
                  wallpaperManager.suggestDesiredDimensions(width, height);
                    Toast.makeText(this, "Wallpaper Set", Toast.LENGTH_SHORT).show();
                 } catch (IOException e) {
                  e.printStackTrace();
                 }
    

    enter link description here

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.