从谷歌照片中选择照片会破坏我的应用程序

问题描述 投票:1回答:2

我正在java上编写一个Android应用程序,需要让我的用户从库中选择和裁剪图像。从任何本地图库中选择图像都没有问题,但是当用户选择吃掉裁剪或从谷歌照片应用程序中选择图像时,应用程序就会崩溃。

我无法弄清楚问题的根源是什么,所以任何答案都会有所帮助

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

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;
    }
}

谢谢你的帮助......

java android google-photos
2个回答
0
投票

Android不支持裁剪意图,因为裁剪不是android api的一部分。 qazxsw poi


0
投票

问题在于作物意图。我最终使用了uCrop库并修复了问题。

© www.soinside.com 2019 - 2024. All rights reserved.