如何使用代码在Android中设置壁纸?

4

我正在开发一个应用程序,可以显示来自服务器的不同照片,用户可以将所选照片设置为其设备的壁纸。我使用了给定的代码来设置壁纸,它可以正常工作,但图像未正确设置,它无法适应屏幕。我使用了以下代码。

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();
}
2个回答

32

要在Android中设置壁纸,请使用以下代码:使用WallpaperManager类

Button buttonSetWallpaper = (Button)findViewById(R.id.set);
ImageView imagePreview = (ImageView)findViewById(R.id.preview);
imagePreview.setImageResource(R.drawable.five);
 
buttonSetWallpaper.setOnClickListener(new Button.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        WallpaperManager myWallpaperManager 
        = WallpaperManager.getInstance(getApplicationContext());
        try {
            myWallpaperManager.setResource(R.drawable.five);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
});

需要在清单文件中设置权限:

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

如果我想将Picasso加载的项目设置为壁纸怎么办? - Fernando Torres

5
你可以尝试像这样调整位图大小。
DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int height = displayMetrics.heightPixels;
    int width = displayMetrics.widthPixels << 1; // best wallpaper width is twice screen width

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, width, height);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    Bitmap decodedSampleBitmap = BitmapFactory.decodeFile(path, options);

    WallpaperManager wm = WallpaperManager.getInstance(this);
    try {
        wm.setBitmap(decodedSampleBitmap);
    } catch (IOException e) {
        Log.e(TAG, "Cannot set image as wallpaper", e);
    }

您可以将 calculateInSampleSize 类编写成这样:
public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}

请参考此链接以获取更多关于加载位图的解释。


计算采样大小(options,width,height); 这是什么? 这个函数显示错误,你没有在你的代码中提及这个函数。 - nishitpatel
人们什么时候开始将位移操作作为标准使用的呢? :) - Phani Rithvij

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接