简体   繁体   English

为什么Android作物意图不返回ActivityResult?

[英]Why doesn't Android crop intent return ActivityResult?

I'm trying to crop an image from the media gallery. 我正在尝试从媒体库中裁剪图像。 I'm able to access the image, start the default crop tool and even save the cropped image result. 我可以访问图像,启动默认裁剪工具甚至保存裁剪的图像结果。

However, the intent I'm using just will not return any results if the cropping was successful. 但是,如果裁剪成功,我正在使用的意图将不会返回任何结果。

My MAIN Method: 我的主要方法:

// Crop photo intent.
Intent intent = new Intent("com.android.camera.action.CROP", null);         
intent.setData(uri);   
intent.putExtra("outputX", outputX);
intent.putExtra("outputY", outputY);
intent.putExtra("aspectX", aspectX);
intent.putExtra("aspectY", aspectY);
intent.putExtra("scale", scale);
intent.putExtra("return-data", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, createTempCroppedImageFile());
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());

// Publish intent to crop picture.
activity.startActivityForResult(intent, activity.CROP_PHOTO_REQUEST_CODE);  

– OnActivityResult() Method – - OnActivityResult()方法 -

// Photo crop activity.
if (requestCode == CROP_PHOTO_REQUEST_CODE) {
    if (resultCode == RESULT_OK) {
        Log.d(TAG, "user cropped a photo");
        signupScreen.setImage(new PhotoTool(this).getTempCroppedImageFileLocation());
    } else
        Log.d(TAG, "user cancelled photo crop");
}

If I cancel out of the crop activity, I get the "user cancelled photo crop" message successfully. 如果我取消了裁剪活动,我会成功获得“用户取消的照片裁剪”消息。 But if I crop an image, the new cropped image will appear in the target directory but the OnActivityResult() function never ever gets called. 但是,如果我裁剪图像,新裁剪的图像将出现在目标目录中,但OnActivityResult()函数永远不会被调用。 What gives? 是什么赋予了?

Looking at the LogCat, I'm finding that every time I save the cropped image, "JavaBinder" complains "Failed Binder Transaction". 看看LogCat,我发现每次保存裁剪的图像时,“JavaBinder”都会抱怨“Binding Transaction失败”。 I understand this is somehow related to memory, but the cropped file is only 24KB in size. 我知道这与内存有某种关系,但裁剪文件的大小只有24KB。

Found the problem to this issue. 找到了这个问题的问题。 It's unfortunately an Android limitation. 不幸的是,这是一个Android限制。

See Android cropper cannot go beyond 256? 看到Android cropper不能超过256?

I had set the output of my cropped image to 400x400. 我已将裁剪图像的输出设置为400x400。 Unfortunately Android's default cropper can only do 256x256. 不幸的是Android的默认裁剪器只能做256x256。 This is so utterly frustrating, especially when there's no documentation on limits. 这非常令人沮丧,特别是当没有关于限制的文档时。

Are you calling setResult(int) ( link ) in the crop intent? 你在裁剪意图中调用setResult(int)link )吗?

UPDATE :\\ 更新 :\\

Sending RESULT_CANCELLED is the default. 发送RESULT_CANCELLED是默认值。 It will always be sent back unless you implement a few things. 它会一直被发回,除非你实现了一些东西。

In your crop intent, when you have the finished the editing (ie the user taps the done button, or something similar), part of that process would call setResult(RESULT_OK) . 在您的裁剪意图中,当您完成编辑(即用户点击完成按钮或类似的东西)时,该过程的一部分将调用setResult(RESULT_OK)

example: 例:

public void doneBTNPressed( View view ) {
    if ( view.getId() == R.id.doneBTN ) {
        this.isCropped = true;
    }
}

@Override    
protected void onPause() {
        if ( this.isCropped ) {
            setResult(RESUL_OK);
        }
        super.onPause();
    }

That result (RESULT_OK, plus another integer you specify) gets passed back to the calling Activity (assuming startActivityForResult() was used). 该结果(RESULT_OK,加上您指定的另一个整数)将传递回调用Activity(假设使用了startActivityForResult())。 There also a few overrides, depending on your needs. 根据您的需要,还有一些覆盖。 In the calling (parent) activity, you would have to implelement onActivityResultint,int,Intent) to get the result from the exiting activity. 在调用(父)活动中,您必须实现onActivityResultint,int,Intent)以从退出活动中获取结果。

In the link provided above, there is a section titled "Starting Activities and Getting Results," which explains the multiple ways to get information out of an exiting actvity. 在上面提供的链接中,有一个标题为“开始活动和获得结果”的部分,它解释了从现有活动中获取信息的多种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM