从Android 4.4中的Gallery中选择裁剪

前端之家收集整理的这篇文章主要介绍了从Android 4.4中的Gallery中选择裁剪前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

@H_502_5@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,以确保我在返回时可以使用它:

@H_502_5@@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指定的文件.有没有新方法这样做?

@H_502_21@
最佳答案@H_502_21@
您可能希望使用返回的intent的数据Uri.

@H_502_5@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.一个意图是选择一张照片,然后一个意图来裁剪该照片.这是更可靠的,因为一些应用程序,如照片应用程序,不知道如何处理“作物”额外.

@H_502_21@

猜你在找的Android相关文章