裁剪图像而不创建新文件

Roh*_*ish 1 android crop bitmap android-intent

我有这段代码从图库中获取图像,然后将其传递给意图进行裁剪.一切工作正常,但这个无论是在画廊创造了新的裁剪图像或裁剪一个替换旧的形象,但我想要做的就是让新的裁剪后的图像在我的节目临时存储,直到它再次被用户更改.

这是我的代码:

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);
Run Code Online (Sandbox Code Playgroud)

Ora*_*bîg 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();
Run Code Online (Sandbox Code Playgroud)