1

I spent hours on an issue that doesn't seem complicated, but despite all the existing messages on this topic, I still don't find a solution...

I'm using a viewpager and in the adapter I have a button and a imageview. I want to set the picture from the imageview as wallpaper when the button is clicked by using myWallpaperManager.setBitmap(image). Before that I resize the picture to fit in screen size. The wallpaper is changed but the picture stays zoomed and cropped. I would like to set the wallpaper with the whole picture. I have the same values for width and height of getDefaultDisplay() and width and height of myWallpaperManager.getDesiredMinimumHeight/Width().

I tried many different ways that didn't work. I don't know what else to do ! Thanks for a lot for your answer.

Here is the code :

 Button ButWallpaper = (Button) viewLayout.findViewById(R.id.ButWallpaper);

      ButWallpaper.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View arg0) {
               Bitmap image;
               Bitmap imagelarge = ((BitmapDrawable)imageView2.getDrawable()).getBitmap();

              int Measuredwidth = 0;  
              int Measuredheight = 0;  
              Point size = new Point();
              WindowManager w = _activity.getWindowManager();

             if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)    {
                 w.getDefaultDisplay().getSize(size);
                 Measuredwidth = size.x;
                 Measuredheight = size.y; 
             }else{
                 Display d = w.getDefaultDisplay(); 
                 Measuredwidth = d.getWidth(); 
                 Measuredheight = d.getHeight(); 
             }

             Log.i("TAG", String.valueOf(Measuredwidth));
             Log.i("TAG", String.valueOf(Measuredheight));

            image =  Bitmap.createScaledBitmap(imagelarge, Measuredwidth, Measuredheight,true);


               // TODO Auto-generated method stub
            WallpaperManager myWallpaperManager 
            = WallpaperManager.getInstance(_activity.getApplicationContext());
            try {
                myWallpaperManager.setBitmap(image);
                 myWallpaperManager.suggestDesiredDimensions(Measuredwidth, Measuredheight);

                 Log.i("TAG", String.valueOf(myWallpaperManager.getDesiredMinimumHeight()));
                 Log.i("TAG", String.valueOf(myWallpaperManager.getDesiredMinimumWidth()));

                 Toast.makeText(_activity.getApplicationContext(), "Wallpaper changed", toast.LENGTH_LONG).show();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }});
tienow
  • 171
  • 12

2 Answers2

1

You can try my code :

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;
            Bitmap tempbitMap = BitmapFactory.decodeFile(path);
            Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap,width,height, true);
            WallpaperManager wallpaperManager = WallpaperManager.getInstance(mContext); 
            wallpaperManager.setWallpaperOffsetSteps(1, 1);
            wallpaperManager.suggestDesiredDimensions(width, height);
            try {
              wallpaperManager.setBitmap(bitmap);
              Toast.makeText(mContext, "Set wallpaper success", Toast.LENGTH_SHORT).show();
              } catch (IOException e) {
              e.printStackTrace();
            }
        }catch(Exception e){
        Log.e("DATA", "Cannot set image as wallpaper", e);
        }

it's ok! And you add permission:

 <uses-permission android:name="android.permission.SET_WALLPAPER" />
 <uses-permission android:name="android.permission.SET_WALLPAPER_HINTS" />
Thientvse
  • 1,753
  • 1
  • 14
  • 23
0

See if this helps:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels; 
Bitmap bitmap = Bitmap.createScaledBitmap(yourimagebitmap, width, height, true);
WallpaperManager.getInstance(MyActivity.this).setBitmap(bitmap);  

Source: Android setting wallpaper gone wrong

Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221
  • Hi, thanks for your answer. Unfortunately, it didn't change anything, I still have the picture cropped... Is there a possibility that it's due to the last updates of Android? – tienow Dec 07 '14 at 22:58
  • http://stackoverflow.com/questions/22759358/wallpaper-not-properly-fit-on-device-screen – An SO User Dec 08 '14 at 04:51