¿Cómo puedo configurar Wallpaper en Android usando codificación?

Estoy desarrollando una aplicación que muestra las diferentes fotos del servidor y el usuario puede configurar las fotos seleccionadas como fondo de pantalla de su dispositivo. Usé un código dado para que el fondo de pantalla funcionara, pero la imagen no se configuró correctamente, no se ajusta a la pantalla. Utilicé este código.

            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();
        }

Por favor, ayúdame.

Respuestas a la pregunta(2)

Su respuesta a la pregunta