Android 4.3 crop gallery resultCode 取消

标签 android gallery crop

我的 Galaxy Nexus 现在在 Android 4.3 上运行,允许我使用这个新版本测试我的应用程序。除了裁剪,一切似乎都很好。

我有一个应用程序使用相机拍照,然后通过图库应用程序裁剪图像。

我还可以从图库中选择一张图片,然后对其进行裁剪。 从 Android 4.3 开始,图库应用发生了变化。

如果我用相机 api 拍照,然后要求图库在我的 onActivityResult 方法中裁剪它,resultCode 设置为 0(意思是取消),而我从裁剪 View 。

但是如果我从图库中选择一张图片并裁剪它一切正常,resultCode 参数设置为 -1。 在这两种情况下,我都调用了相同的方法来裁剪图片。

我的手机上有 quickpic(图库应用程序的替代品),一切正常!

private void performCrop(Uri picUri) {
    try {
        int aspectX = 750;
        int aspectY = 1011;

        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(picUri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("scale", "true");
        intent.putExtra("aspectX", aspectX);
        intent.putExtra("aspectY", aspectY);
        intent.putExtra("scaleUpIfNeeded", true);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCurrentPhotoPath)));

        startActivityForResult(intent, CROP);
    }
    catch (ActivityNotFoundException anfe) {
        String errorMessage = "Your device doesn't support the crop action!";
        Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
        toast.show();
    }
}

在 Android 4.2.2 上一切正常。 感谢您的帮助!

最佳答案

您是否考虑过只使用像这样的库:

https://github.com/biokys/cropimage

我发现 com.android.camera.action.CROP 有时会因手机而异,而且并不总是可用,因此如果您要发布它,它可能会给您带来一些问题。

更新:

我已经用 Android 4.3 测试了上面的库,它没有问题。您只需将库添加到您的项目中。

然后您可以用非常相似的方式编写您的方法:

private void performCrop(Uri picUri) {
//you have to convert picUri to string and remove the "file://" to work as a path for this library
String path = picUri.toString().replaceAll("file://", "");

try {
    int aspectX = 750;
    int aspectY = 1011;

    Intent intent = new Intent(this, CropImage.class);
    //send the path to CropImage intent to get the photo you have just taken or selected from gallery
    intent.putExtra(CropImage.IMAGE_PATH, path);

    intent.putExtra(CropImage.SCALE, true);

    intent.putExtra("aspectX", aspectX);
    intent.putExtra("aspectY", aspectY);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCurrentPhotoPath)));

    startActivityForResult(intent, CROP);
}
catch (ActivityNotFoundException anfe) {
    String errorMessage = "Your device doesn't support the crop action!";
    Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
    toast.show();
}

关于Android 4.3 crop gallery resultCode 取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17930577/

相关文章:

javascript - 使用 D3 样条线时如何阻止图形被裁剪?

android - 为什么更改默认按钮的颜色使其在 Android 中看起来像矩形?

android - 使用 Parse.com 和 Android 两次自定义推送通知

Android:图片库分享到应用程序

css - 在 div 或其 <ul> 中居中 float 列表项 <li>

c++ - 拼接后裁剪图像

python - 在 python 中使用 opencv 剪切图像的特定部分

java - UUID 蓝牙和安卓设备

java - 如何对发送到 Activity 的不同 Extras 执行不同的操作?

android - 从 Assets 文件夹加载图像时图库显示空白