0

My problem is the image previewed on the imageview is not able to set fully as background wallpaper. Some part of it cuts. this is my onclick of button

    setasW.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
              mImage.buildDrawingCache();
              Bitmap bmap = mImage.getDrawingCache();

              float scaleWidth = ((float) width) / bmap .getWidth();
            float scaleHeight = ((float) height) / bmap .getHeight();
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);

              Bitmap scaledBitmap = Bitmap.createBitmap(bmap, 0, 0,bmap .getWidth(), bmap .getHeight(), matrix, true);;
              System.out.println("scaledBitmap-------"+scaledBitmap);
             WallpaperManager m=WallpaperManager.getInstance(getApplicationContext());
             try {
                m.setBitmap(scaledBitmap);
             } catch (IOException e) {
                e.printStackTrace();
             }
        }
     });

here mimage is the imageview Width and height are the screen size.

How to set the full image as wallpaper?

Wishy
  • 391
  • 2
  • 6
  • 18

1 Answers1

0

You can use this directly I have made necessary changes

     setasW.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

     DisplayMetrics outMetrics=new DisplayMetrics();
                            getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
                            int w=outMetrics.widthPixels;
                            int h=outMetrics.heightPixels;


Rect rectgle= new Rect();
Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight= rectgle.top;
int contentViewTop= 
    window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int TitleBarHeight= contentViewTop - StatusBarHeight;



                      mImage.buildDrawingCache();
                      Bitmap bmap = mImage.getDrawingCache();

                      bmap =Bitmap.createScaledBitmap(bmap ,w,(h-TitleBarHeight), true);
                      System.out.println("scaledBitmap-------"+scaledBitmap);
                     WallpaperManager m=WallpaperManager.getInstance(getApplicationContext());
                     try {
                        m.setBitmap(scaledBitmap);
                     } catch (IOException e) {
                        e.printStackTrace();
                     }
                }
             });
Aashish Bhatnagar
  • 2,595
  • 2
  • 22
  • 37