11

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-

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" />
John R
  • 2,078
  • 8
  • 35
  • 58
  • 1
    i've also face same problem of zooming wallpaper. What you find solution for this please help me for solving this [problem] (http://stackoverflow.com/questions/32201161/wallpaper-not-properly-fit-on-samsung-devices) .... – Garg Aug 25 '15 at 11:16

5 Answers5

19

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>
PankajSharma
  • 1,529
  • 14
  • 27
  • Hii @John R if you are satisfied with my code then accept my answer..otherwise let me know please... – PankajSharma Apr 09 '14 at 05:02
  • You did this so late.bounties are expired.. :( i loose 100 reputation s.@John R – PankajSharma Apr 10 '14 at 05:50
  • I found one difficulty in your code if i restart my device or after playing game back to home screen size of wallpaper increases. How to stop this? – John R Apr 14 '14 at 04:03
  • increase in the sense??the size of wallpaper became zoomed or anything else? – PankajSharma Apr 14 '14 at 04:10
  • And i think both the code here you gave the bounties and mine code are same and both are working same. – PankajSharma Apr 14 '14 at 04:15
  • Is there any way to stop it zooming? I want permanent look as in above image. – John R Apr 14 '14 at 04:16
  • 1
    you answered your own question. why I awarded that bounty to that user. Because your and his answer are same and he answered before you. – John R Apr 14 '14 at 04:21
  • Please dont mind. This site is for helping others. Thats more valuable thing than points and I think you earned 95 points from this answer. Now check its 105 more than bounty. – John R Apr 14 '14 at 04:22
  • thanks.its ok.it does'nt matter.u can join android coders chat room i m there can talk better. – PankajSharma Apr 14 '14 at 04:26
  • please tell me the solution. – John R Apr 14 '14 at 06:00
  • but what about other problem if I play a game and then back to home screen in this case image size zoom also. – John R Apr 14 '14 at 09:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/50774/discussion-between-john-r-and-android-guru) – John R Apr 16 '14 at 10:52
  • 1
    @Pankaj Sharma i've also face this problem but this code not work for me please guide me http://stackoverflow.com/questions/32201161/wallpaper-not-properly-fit-on-samsung-devices – Garg Aug 25 '15 at 11:28
3

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();
    }
RQube
  • 954
  • 3
  • 13
  • 28
  • what issue you are facing – RQube Apr 16 '14 at 10:50
  • On restarting device or after playing a game when I back to home screen. Image get zoom. – John R Apr 16 '14 at 10:55
  • device restart could be handled by the answer of android guru, but change in device orientation would have same effect you are getting after playing a game. Do you have any app doing the same task as you require? – RQube Apr 16 '14 at 12:34
  • code by AndroidGuru not working on device restart.And I have fixed orientation its portrait. – John R Apr 18 '14 at 14:28
  • @JohnR I think we have missed something very important about the method suggestDesiredDimensions() : "For use only by the current home application, to specify the size of wallpaper it would like to use. This allows such applications to have a virtual wallpaper that is larger than the physical screen, matching the size of their workspace. This is for home screens to tell what size wallpaper they would like. Nobody else should be calling this! Certainly not other non-home-screen apps that change the wallpaper. " refer to description of method suggestDesiredDimensions () – RQube Apr 21 '14 at 12:44
  • Read description of suggestDesiredDimensions() method carefully here http://developer.android.com/reference/android/app/WallpaperManager.html#suggestDesiredDimensions%28int,%20int%29 – RQube Apr 30 '14 at 12:17
  • So what's the solution for this? – John R May 01 '14 at 02:24
2

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.

Harshit Rathi
  • 1,862
  • 2
  • 18
  • 25
0

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..

Lokesh
  • 5,180
  • 4
  • 27
  • 42
0

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

lingyfh
  • 1,363
  • 18
  • 23