设备屏幕上的壁纸不正确

我在一个可绘制的文件夹中有一组图像。 我有一个button在设备屏幕上将图像设置为壁纸。 但是,当我把这个图像设置为墙纸,无论是缩放或裁剪。 我希望图像应该适合屏幕大小。 我已经看到了很多链接,但没有链接是为我工作。 这是我到目前为止的代码。

码-

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

我还在manifest中增加了以下几行:

 <uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/> <uses-permission android:name="android.permission.SET_WALLPAPER" /> 

Solutions Collecting From Web of "设备屏幕上的壁纸不正确"

你好这是与可绘制的图像我已经检查了..

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

还要在Manifest.xml中提及这些权限。

  <uses-permission android:name="android.permission.SET_WALLPAPER_HINTS" /> <uses-permission android:name="android.permission.SET_WALLPAPER" /> 

这是截图

在这里输入图像说明

为了重置屏幕上的适应壁纸存储在共享偏好的图像path,并使用启动完成接收器,然后重置屏幕上的同一壁纸….

广播接收机是..

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

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

试试这个代码

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

如果您有图片url,请使用:

 WallpaperManager wpm = WallpaperManager.getInstance(context); InputStream ins = new URL("absolute/path/of/image").openStream(); wpm.setStream(ins); 

如果你有图像的URI,然后使用

 WallpaperManager wpm = WallpaperManager.getInstance(context); wpm.setResource(Uri.of.image); 

在你的清单文件中:

 <uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission> 

不要忘记把这个代码放在AsyncTask

试试这个代码:

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

在这个例子中,我用我的device SD卡中的图像…

这对我来说是完美的

注意:也许不适合你。

你需要这个:

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

在这里input链接描述