2012-08-31 26 views
1

我有这段代码从图库中获取图像,然后将其传递给意图裁剪。一切工作正常,但这或者在图库中创建新的裁剪图像或用剪裁的图像替换旧图像,但我想要做的是将新的裁剪图像保存在我的程序临时内存中,直到用户再次更改。裁剪图像而不创建新文件

这里是我的代码:

Uri selectedImage = imageReturnedIntent.getData(); 

final Intent intent = new Intent("com.android.camera.action.CROP"); 
       intent.setData(selectedImage); 
       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("output", selectedImage); // with this enabled it replaces the original image and without it creates new one in gallery. 
       startActivityForResult(intent, 23); 

回答

5

你应该创建一个临时文件,你会事后删除:

(...) 
    File tempFile = File.createTempFile("crop", "png", Environment 
              .getExternalStorageDirectory()); 
    Uri tempUri = Uri.fromFile(tempFile); 
    intent.putExtra("output", tempUri); 
    intent.putExtra("outputFormat", "PNG"); 
    (...) 

    // then, when the intent returns 

    // read cropped image from tempUri 
    (...) 
    // finally delete the temp file 
    tempFile.delete();