【发布时间】: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