android - 在不创建新文件的情况下裁剪图像

标签 android android-intent bitmap crop

我有这段代码可以从图库中获取图像,然后将其传递给 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);

最佳答案

您应该创建一个临时文件,之后您将删除该文件:

    (...)
    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();

关于android - 在不创建新文件的情况下裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12222020/

相关文章:

android - appcompat-v7更新取消Toolbar文字颜色自定义

android - 尝试从 onActivityResult 中获取数据时出现 NullPointerException

android - 在 Android 中向内存中的位图添加文本

Android位图形菱形

android - React Native onLayout nativeEvent.layout.y 总是返回 0

android - 如何在 Xamarin.forms 中更改视频的分辨率

android - 当应用程序启动 (a) 从 android 市场 (b) 然后回家时返回完整的后台

android - 强制相机始终在 android 中以纵向模式打开

c - 使用 calloc 在 C 中创建位图

android - 当我单击微调器时如何从微调器中删除箭头?