壁纸不适合设备屏幕

Joh*_*n R 10 android wallpaper

我在一个drawable文件夹中有一组图像.我有一个按钮将图像设置为设备屏幕上的壁纸.但是,当我将此图像设置为壁纸时,无论是缩放还是裁剪.我想图像应该适合屏幕尺寸.我在SO上看过很多链接,但没有链接对我有用.这是我到目前为止所尝试的代码.

码-

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();
}
Run Code Online (Sandbox Code Playgroud)

我还在清单中添加了以下行:

<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>
<uses-permission android:name="android.permission.SET_WALLPAPER" />
Run Code Online (Sandbox Code Playgroud)

Pan*_*rma 23

您好这是使用drawable图像我检查了..

                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();
                }
Run Code Online (Sandbox Code Playgroud)

另请在Manifest.xml中提及这些权限.

    <uses-permission android:name="android.permission.SET_WALLPAPER_HINTS" />
    <uses-permission android:name="android.permission.SET_WALLPAPER" />
Run Code Online (Sandbox Code Playgroud)

这是截图..

在此输入图像描述

对于重置适合壁纸屏幕存储共享首选项中的图像路径并使用启动完成接收器然后重置屏幕上相同的壁纸....

广播接收器是......

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());
    }
}
}
Run Code Online (Sandbox Code Playgroud)

在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>
Run Code Online (Sandbox Code Playgroud)