title: How can I set Wallpaper in android using code?
body: <p>I am developing an application that shows the different photos from server and user can set selected photos as wallpaper of its device i used given code to set wallpaper it working but image was not set properly it not fit to screen. I used this code.</p>
  
  <pre><code>            String dirPath = getFilesDir().toString();
  
          String folder = mPhotos.get(nextPosition - 1).getCategory();
          String filePath = dirPath + "/PhotoViewer/" + folder + "/"
                  + mPhotos.get(nextPosition - 1).getFileName();
          File imageFile = new File(filePath);
          Bitmap bitmap = BitmapFactory.decodeFile(imageFile
                  .getAbsolutePath());
          WallpaperManager myWallpaperManager = WallpaperManager
                  .getInstance(getApplicationContext());
          try {
              myWallpaperManager.setBitmap(bitmap);
              Toast.makeText(PhotoActivity.this, "Wallpaper set",
                      Toast.LENGTH_SHORT).show();
          } catch (IOException e) {
              Toast.makeText(PhotoActivity.this, "Error setting wallpaper",
                      Toast.LENGTH_SHORT).show();
          }
  </code></pre>