selecting photos from google photos crushes my app

问题: i'm writing an android app on java and need to let my users select and crop images from the gallery. There is no problem when choosing an image from any native gallery, but...

问题:

i'm writing an android app on java and need to let my users select and crop images from the gallery. There is no problem when choosing an image from any native gallery, but when a user chooses to eater crop or choose an image from google photos app the app crushes.

I cannot figure out what is the source of the problem so any answer will be helpful

this is the code i'm using
class fields:

private Uri imageUri;

opening the camera:

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

opening the gallery:

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

cropping the image:

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) {}
}

the result handler:

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

thank you for any useful help...


回答1:

Android does not support crop intent because croping is not part of android api. So i recommend you for Using library

  • 发表于 2019-02-18 20:21
  • 阅读 ( 267 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除
ndow.open(qcl); }, function(){ window.open(ali); }); window.localStorage.setItem(key,"has"); } })