从Android 4.4中的Gallery中选择裁剪

 dendfengg_566 发布于 2023-02-11 08:31

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

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有