微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从Android 4.4中的Gallery中选择裁剪

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

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已经创建了带有croped图像的文件.

现在,当我这样做时,从未创建由意图中的photoUri指定的文件.有没有新方法这样做?

最佳答案
您可能希望使用返回的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.一个意图是选择一张照片,然后一个意图来裁剪该照片.这是更可靠的,因为一些应用程序,如照片应用程序,不知道如何处理“作物”额外.

原文地址:https://www.jb51.cc/android/430969.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐