简体   繁体   English

requestCode = -1和resultCode = 0选择器来自图库或相机的图像的意图

[英]requestCode = -1 and resultCode = 0 chooserIntent for image from gallery or camera

So I managed to create a chooserIntent copying and pasting from this link the bounty-awarded answer. 因此,我设法通过这个链接创建了一个ChooserIntent 复制和粘贴 赏金奖的答案。 The problem I am facing is in the onActivityResult method. 我面临的问题是在onActivityResult方法中。 The requestCode that I receive is -1 and the resultCode is 0. What is wrong with the code? 我收到的requestCode为-1,resultCode为0。代码有什么问题?

Starting intent: 开始意图:

private void openImageIntent() {

        // Determine Uri of camera image to save.
        final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir" + File.separator);
        root.mkdirs();
        final String fname = "img_" + System.currentTimeMillis() + ".jpg";
        final File sdImageMainDirectory = new File(root, fname);
        outputFileUri = Uri.fromFile(sdImageMainDirectory);

        // Camera.
        final List<Intent> cameraIntents = new ArrayList<Intent>();
        final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        final PackageManager packageManager = getPackageManager();
        final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
        for(ResolveInfo res : listCam) {
            final String packageName = res.activityInfo.packageName;
            final Intent intent = new Intent(captureIntent);
            intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
            intent.setPackage(packageName);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            cameraIntents.add(intent);
        }

        // Filesystem.
        final Intent galleryIntent = new Intent();
        galleryIntent.setType("image/*");
        galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

        // Chooser of filesystem options.
        final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");

        // Add the camera options.
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));

        startActivityForResult(chooserIntent, 0);
    }

The onActivityResult : onActivityResult

@Override
    protected void onActivityResult(int resultCode, int requestCode, Intent returnIntent) {
        super.onActivityResult(resultCode, requestCode, returnIntent);
        if(requestCode == 0) {
            if(resultCode == RESULT_OK) {
                final boolean isCamera;
                if(returnIntent == null)
                {
                    isCamera = true;
                }
                else
                {
                    final String action = returnIntent.getAction();
                    if(action == null)
                    {
                        isCamera = false;
                    }
                    else
                    {
                        isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    }
                }

                Uri selectedImageUri;
                if(isCamera)
                {
                    selectedImageUri = outputFileUri;
                    ivProfilePicture.setImageURI(selectedImageUri);
                    makeToast(selectedImageUri.toString(), false);

                }
                else
                {
                    selectedImageUri = returnIntent == null ? null : returnIntent.getData();
                    makeToast(selectedImageUri.toString(), false);
                }
            }
        }
    }

RESULT_OK=-1 per the Activity definition. 根据活动定义, RESULT_OK=-1 You specified the request code as 0, so that also looks fine. 您将请求代码指定为0,因此看起来也不错。 Your problem: You are mixing up the requestCode and resultCode, as is specified for the Activity class .. Try this: 您的问题:您要混合使用Activity类指定的requestCode和resultCode。

protected void onActivityResult(int requestCode, int resultCode, Intent returnIntent) 受保护的void onActivityResult(int requestCode,int resultCode,Intent returnIntent)

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

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