【问题标题】:Android crop intent doesn't work on system gallery app but works on 3rd-party appAndroid 裁剪意图不适用于系统图库应用,但适用于 3rd 方应用
【发布时间】:2014-04-17 14:41:18
【问题描述】:

我尝试从 sdcard 中挑选一张图片,然后对其进行裁剪。 ACTION_PICK 没问题,但是当我调用 ACTION_CROP 时,我的系统图库应用程序(我称之为 A)无法执行该操作,但另一个应用程序 (B) 可以。
我尝试了以下情况:
1/ 按 A 选择然后按 A 裁剪 => 选择 OK,裁剪失败
2/ 按 B 选择然后按 A 裁剪 => 与第一种情况相同。
3/ 按 A 挑选,然后按 B 裁剪 => 一切正常。
4/ 由 B 挑选,然后由 B 裁剪 => 一切正常。
所以我的临时结论是:我的系统应用程序无法使用我的代码执行裁剪操作(可能是我忘记了什么)。这是我的代码:
ACTION_PICK:

public Intent galleryIntent() {
    Intent galleryIntent = new Intent(Intent.ACTION_PICK);
    galleryIntent.setType("image/*");
    galleryIntent.putExtra("return-data", true);
    return galleryIntent;
}


ACTION_CROP:

public Intent cropIntent(Uri inUri, int outputX, int outputY,
        boolean isScale) {
    Intent cropIntent = new Intent("com.android.camera.action.CROP");
    cropIntent.setDataAndType(inUri, "image/*");
    cropIntent.putExtra("crop", "true");
    cropIntent.putExtra("aspectX", outputX);
    cropIntent.putExtra("aspectY", outputY);
    cropIntent.putExtra("outputX", outputX);
    cropIntent.putExtra("outputY", outputY);
    cropIntent.putExtra("scale", isScale);
    cropIntent.putExtra("return-data", true);
    return cropIntent;
}


我的 onActivityResult 方法

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
        case REQUEST_CODE_GALLERY:
            imageUri = data.getData();
            startActivityForResult(cropIntent(imageUri,
                    500, 500, true), REQUEST_CODE_CROP);
            break;
        case REQUEST_CODE_CROP:
            Bundle extras = data.getExtras();
            Bitmap tempBitmap = extras.getParcelable("data");
            imgvMain.setImageBitmap(null);
            imgvMain.setImageBitmap(tempBitmap);
            break;
        }
    } else {
        imageUri = null;
    }
}


我错过了什么吗?
感谢您的关注!

【问题讨论】:

  • 我也面临同样的问题。当我选择该应用程序进行裁剪时,图库应用程序崩溃。你应用了什么解决方案?
  • @CodeKrish 试试这个:github.com/biokys/cropimage
    如果这对你有帮助,别忘了给我的问题投票:P
  • 我能够找到原因并修复它,而无需创建自己的裁剪功能。我已经发布了答案。请看一看。

标签: crop android-gallery


【解决方案1】:

我成功地将此代码用于 Android 2.2 及更高版本:

它会打开一系列可以获取图像文件的应用程序,例如图库应用程序。如果所选应用可以裁剪,它也会这样做。

裁剪后的图像将保存到提供的临时文件中。

(注意 KITKAT 的细微差别)。

        Intent intent = new Intent();

        intent.setType("image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("outputX", Constants.IMAGE_WIDTH); 
        intent.putExtra("outputY", Constants.IMAGE_HEIGHT); 
        intent.putExtra("aspectX", 1); 
        intent.putExtra("aspectY", 1); 
        intent.putExtra("scale", true); 
        intent.putExtra("scaleUpIfNeeded", true);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(<a temp file created somewhere>));
        intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
        {
            intent.setAction(Intent.ACTION_GET_CONTENT);
        } 
        else 
        {
            intent.setAction(Intent.ACTION_PICK);
            intent.setData(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        }

        startActivityForResult(intent, RESULT_CROP);

编辑: 我最终使用了自定义裁剪:https://github.com/biokys/cropimage。这很容易,我在裁剪方面没有更多麻烦:-)

【讨论】:

  • 请弄清楚你和我有什么不同,我想没有什么不同。
  • 我解释了如何处理 KITKAT...除此之外你做得很好
  • 也许你应该像我一样做。我使用:https://github.com/biokys/cropimage 添加了自定义裁剪。这很容易,我没有更多的裁剪问题:-)
【解决方案2】:

我通过使用 gallery3d 过滤检查了日志,发现股票应用没有获得访问 uri 的权限。因此它的行为出乎意料。对于不同的平台,这种行为是不同的。

解决方案:

  1. 在 onActivityResult() 中为 Intent ACTION_PICK 获取所选图像的 uri。
  2. 暂时保存图片。
  3. 从保存的图像创建新的 URI。
  4. 将新的 URI 传递给 com.android.camera.action.CROP。

示例代码:(不要复制粘贴。为简单起见,我删除了错误检查和异步任务。)

public void pickCroppedPhoto(){
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    photoPickerIntent.putExtra("crop", "true");
    populateCropExtras(activity, photoPickerIntent);
    startActivityForResult(photoPickerIntent , REQUEST_CODE_PICK);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
    if (requestCode == REQUEST_CODE_PICK ) {
        File mainImage = saveUriInFile( this,
                                    data.getData(),
                                    getTempFileForMainImage());
        if (null == mainImage) {
            handleImageSelectionFailure();
        }else {
            try {
               Uri mainFileUri = Uri.fromFile(mainImage);
               performCrop(this,mainFileUri);
            }catch(Exception e){
                handleImageSelectionFailure();
            }
        }
    }else if ( requestCode == PIC_CROP ){
        postImageSelection(data.getData());
    }
}

这是我的 performCrop 代码,与问题的代码相似。

public static boolean performCrop(Activity activity, Uri picUri) {
    try {

        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        // indicate image type and Uri
        cropIntent.setDataAndType(picUri, "image/*");
        if (populateCropExtras(activity, cropIntent)) return false;
        activity.startActivityForResult(cropIntent, PIC_CROP);
        return true;
    }
    // respond to users whose devices do not support the crop action
    catch (ActivityNotFoundException anfe) {
        String errorMessage = "Device doesn't support crop!";
        Log.w(PhotoPickerUtil.class.getCanonicalName(), errorMessage);
        return false;
    }catch (IOException ioe){
        String errorMessage = "Error while getting temporary file from external storage";
        Log.w(PhotoPickerUtil.class.getCanonicalName(), errorMessage);
        return false;
    }
}

private static void populateCropExtras(Activity activity, Intent cropIntent) throws IOException { {
    // set crop properties
    cropIntent.putExtra("crop", "true");


    // indicate output X and Y
    cropIntent.putExtra("outputX", 300);
    cropIntent.putExtra("outputY", 300);

    // indicate aspect of desired crop
    cropIntent.putExtra("aspectX", 1);
    cropIntent.putExtra("aspectY", 1);
    cropIntent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());

    Uri tempUri = Uri.fromFile(getTempFile(activity));
    cropIntent.putExtra("output", tempUri);
}

【讨论】:

    猜你喜欢
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多