从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);
}
Run Code Online (Sandbox Code Playgroud)
然后我保存了我的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"));
}
Run Code Online (Sandbox Code Playgroud)
然后在onActivityResult中,我只需要使用photoUri打开一个InputStream,因为galleryIntent已经创建了带有croped图像的文件.
现在,当我这样做时,从未创建由意图中的photoUri指定的文件.有没有新方法这样做?