【问题标题】:Crop while selecting from Gallery in Android 4.4在 Android 4.4 中从图库中选择时裁剪
【发布时间】:2013-12-13 11:26:56
【问题描述】:

从 Android 4.4 开始,当您启动 Intent.ACTION_GET_CONTENT 类型的 Intent 时,它不会在 Gallery、Dropbox 等之间进行选择,而是会打开新的文档浏览器。如果您只想打开一个图像,这很好,因为这仍然可以以与旧 APIS 相同的方式执行。当您需要裁剪所选图像时会出现问题,因为文档浏览器忽略了我传递给它的 Uri 和裁剪参数。这就是我正在做的事情:

void take_photo() {
    File file = null;
    try {
        file = PhotoUtils.createTemporaryFile("picture", ".jpg",
                EditProfileActivity.this);
        file.delete();

    } catch (Exception e) {
        e.printStackTrace();
    }
    photoUri = Uri.fromFile(file);
    Intent galleryIntent= new Intent(Intent.ACTION_GET_CONTENT);
    galleryIntent.setType("image/*");
    galleryIntent.putExtra("crop", "true");
    galleryIntent.putExtra("aspectX", 2);
    galleryIntent.putExtra("aspectY", 2);
    galleryIntent.putExtra("outputX", 1300);
    galleryIntent.putExtra("outputY", 1300);
    galleryIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
    startActivityForResult(galleryIntent, ACTIVITY_SELECT_IMAGE);
}

然后我保存了我的 photoUri 以确保在返回时可以使用它:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if (photoUri != null)
        outState.putString("uri", photoUri.toString());
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if (savedInstanceState.containsKey("uri"))
        photoUri = Uri.parse(savedInstanceState.getString("uri"));
}

然后在 onActivityResult 中,我只需要使用 photoUri 打开一个 InputStream,因为 galleryIntent 已使用裁剪后的图像创建了文件。

现在,当我这样做时,不会创建意图中 photoUri 指定的文件。有没有新的方法可以做到这一点?

【问题讨论】:

    标签: android android-intent gallery crop android-4.4-kitkat


    【解决方案1】:

    您可能希望使用返回的 Intent 的数据 Uri。

    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    
      if (requestCode == ACTIVITY_SELECT_IMAGE) {
    
         if(resultCode == RESULT_OK){      
             Uri realUri = intent.getData();   
         }
      }
    }
    

    现在因为 DocumentsActivity 不知道如何“裁剪”任何东西。您可以将您的操作更改为:Intent.ACTION_PICK

    这将使您绕过 DocumentsActivity 直接进入图库或照片应用程序。

    不过,我建议您使用两个 Intent。一个意图是选择一张照片,然后一个意图是裁剪该照片。这更加可靠,因为某些应用程序,例如照片应用程序,也不知道如何处理“裁剪”额外内容。

    【讨论】:

    • 这就是我在 Kitkat 中所做的,但无论如何都不允许使用 crop 参数
    猜你喜欢
    • 1970-01-01
    • 2012-06-02
    • 2020-12-21
    • 1970-01-01
    • 2017-11-02
    • 2013-10-11
    • 2014-10-18
    • 2016-02-17
    相关资源
    最近更新 更多