【问题标题】:Cropping Image without creating new file裁剪图像而不创建新文件
【发布时间】:2012-08-26 15:58:30
【问题描述】:

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

这是我的代码:

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

【问题讨论】:

    标签: android android-intent bitmap crop


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      • 2011-07-14
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      相关资源
      最近更新 更多