WallpaperManager意图裁剪并从位图调整大小

时间:2016-01-23 05:31:40

标签: android bitmap

现在我们正在使用以下代码从一些加载的图像设置壁纸。

bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
     myWallpaperManager.setBitmap(bitmap);
} catch (IOException e) {                                
  e.printStackTrace();
}
Toast.makeText(this, R.string.set_wallpaper_complete_toast,Toast.LENGTH_SHORT).show();
return true;

我们如何在上面的代码中使用getCropAndSetWallpaperIntent(Uri imageUri),以便它会询问选项来设置壁纸并裁剪它。

2 个答案:

答案 0 :(得分:0)

    public Uri getImageUri(Context inContext, Bitmap inImage) {
      ByteArrayOutputStream bytes = new ByteArrayOutputStream();
      inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
      String path = Images.Media.insertImage(inContext.getContentResolver(), 
inImage, "Title", null);
      return Uri.parse(path);
    } 

然后,

bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
WallpaperManager myWallpaperManager
              = WallpaperManager.getInstance(getApplicationContext());
                try { 
                   Uri myImageUri = getImageUri(bitmap);
                    Intent intent = new Intent(myWallpaperManager.getCropAndSetWallpaperIntent(myImageUri));
                } catch (IOException e) {
                    // TODO Auto-generated catch block 
                    e.printStackTrace();
                    return false;
                } 
                Toast.makeText(
                        this, R.string.set_wallpaper_complete_toast,Toast.LENGTH_SHORT).show();
                return true; 

答案 1 :(得分:0)

使用以下内容

val path: String = MediaStore.Images.Media.insertImage(contentResolver, bitmap, "wallpaper.jpg", null)
        val intent = Intent(wallpaperManager.getCropAndSetWallpaperIntent(Uri.parse(path)))
        startActivity(intent)