1

我只是裁剪图像以获取其中的一部分。但是Android将返回的图像设置为墙纸。为什么?我跟踪 android 代码,在 Gallery3D 应用程序(com.cooliris)中,我发现了这个:

    // TODO: A temporary file is NOT necessary
    // The CropImage intent should be able to set the wallpaper directly
    // without writing to a file, which we then need to read here to write
    // it again as the final wallpaper, this is silly
    mTempFile = getFileStreamPath("temp-wallpaper");
    mTempFile.getParentFile().mkdirs();

    int width = getWallpaperDesiredMinimumWidth();
    int height = getWallpaperDesiredMinimumHeight();
    intent.putExtra("outputX", width);
    intent.putExtra("outputY", height);
    intent.putExtra("aspectX", width);
    intent.putExtra("aspectY", height);
    intent.putExtra("scale", true);
    intent.putExtra("noFaceDetection", true);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTempFile));
    intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.name());
    // TODO: we should have an extra called "setWallpaper" to ask CropImage
    // to set the cropped image as a wallpaper directly. This means the
    // SetWallpaperThread should be moved out of this class to CropImage

请关注最后几行,TODO。它告诉作物意图将完成设置工作。好吧,我根本不需要它。那么,如何在不设置壁纸的情况下裁剪图像?谢谢!

4

1 回答 1

1

在您的代码中执行此操作(请记住,它不适用于所有手机,例如 HTC,因为它们使用自己的图库/相机。

File f = new File(Environment.getExternalStorageDirectory(), "/temporary_holder.png");
f.createNewFile();
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setClassName("com.android.gallery", "com.android.camera.CropImage");
intent.setType("image/*"); 
intent.setData(ImageToSetUri);   // Local URI of your image
intent.putExtra("crop", true);
intent.putExtra("outputX", width); // width/height of your image
intent.putExtra("outputY", height);
intent.putExtra("aspectX", width);
intent.putExtra("aspectY", height);
intent.putExtra("scale", true);
intent.putExtra("noFaceDetection", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.name());
startActivityForResult(intent, 1);

然后执行此操作以将图像检索为位图,或者您想要

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK)
          if (data != null) {
              Bitmap selectedImage = BitmapFactory.decodeFile(f);       
          }
}
于 2012-02-02T19:30:33.877 回答