简体   繁体   中英

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. The problem I am facing is in the onActivityResult method. The requestCode that I receive is -1 and the resultCode is 0. What is wrong with the code?

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 :

@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. You specified the request code as 0, so that also looks fine. Your problem: You are mixing up the requestCode and resultCode, as is specified for the Activity class .. Try this:

protected void onActivityResult(int requestCode, int resultCode, Intent returnIntent)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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