android设置我裁剪为壁纸的图像

时间:2011-01-07 04:17:24

标签: android crop wallpaper

我只是裁剪图像以获得它的一部分。但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。它告诉裁剪意图将完成设置工作。好吧,我根本不需要它。那么,如何在没有设置壁纸的情况下制作图像?谢谢!

1 个答案:

答案 0 :(得分: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);       
          }
}