从谷歌照片中选择照片会使我的应用程序崩溃

时间:2019-02-16 20:25:29

标签: java android google-photos

我正在用Java编写一个android应用程序,需要让我的用户从图库中选择和裁剪图像。 从任何本机画廊中选择图像都没有问题,但是当用户选择进食或从google photos应用中选择图像时,该应用就会崩溃。

我无法找出问题的根源,因此任何答案都将有所帮助

这是我正在使用的代码
类字段:

private Uri imageUri;

打开相机:

private void camOpen() {
    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File f = new File(Environment.getExternalStorageDirectory(), "file" + String.valueOf(System.currentTimeMillis()) + ".png");
    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());

    imageUri = Uri.fromFile(f);
    i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    i.putExtra("return-data", true);
    startActivityForResult(i, CAMERA_CODE);
}

打开画廊:

private void galleryOpen() {
    Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(Intent.createChooser(i, "select file"), SELECT_PHOTO_CODE);
    }

裁剪图像:

private void cropImage() {
    try {
        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        cropIntent.setDataAndType(imageUri, "image/*");
        cropIntent.putExtra("crop", true);
        cropIntent.putExtra("aspectX", 1);
        cropIntent.putExtra("aspectY", 1);
        cropIntent.putExtra("return-data", true);
        startActivityForResult(cropIntent, CROP_CODE);

    } catch (Exception e) {}
}

结果处理程序:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CAMERA_CODE && resultCode == Activity.RESULT_OK) {
        cropImage();
    } else if (requestCode == SELECT_PHOTO_CODE && resultCode == Activity.RESULT_OK) {
        if (data != null) {
            imageUri = data.getData();
            cropImage();
        }
    } else if (requestCode == CROP_CODE && resultCode == Activity.RESULT_OK) {
        Bundle bundle = data.getExtras();
        Bitmap b = bundle.getParcelable("data");
        hasImageChanged=true;
        ivProfilePic.setImageBitmap(b);
        capturedImage = b;
    }
}

感谢您提供任何有用的帮助...

2 个答案:

答案 0 :(得分:0)

Android不支持裁剪意图,因为裁剪不是android API的一部分。 So i recommend you for Using library

答案 1 :(得分:0)

问题在于作物的意图。我最终使用了uCrop库,它解决了这个问题。