【问题标题】:selecting photos from google photos crushes my app从谷歌照片中选择照片会破坏我的应用
【发布时间】:2019-02-16 20:25:29
【问题描述】:

我正在用 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;
    }
}

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

【问题讨论】:

  • 我在 Intent 构造函数中使用了“com.android.camera.action.CROP”,它可以很好地裁剪从我的手机图库中选择的图像
  • 大约有 20 亿台 Android 设备。您正在测试 1。请不要假设在您的设备上发生的无证、不受支持的事情会在其他人的设备上运行。

标签: java android google-photos


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    问题在于裁剪意图。我最终使用了 uCrop 库,它解决了问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-13
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-08
      • 2015-10-20
      相关资源
      最近更新 更多