简体   繁体   中英

unable to pass bundle data from image File Chooser intent to onActivityResult

In an android studio 3.4 project activity that has four different buttons that pass different Strings values (flags) when clicked. I am trying to pass a bundle data from a variable string flag in image File Chooser intent to onActivityResult()

There are four different image choosers launched by the four buttons I want my method to do something if a particular string flag value is passed but my string keeps getting passed as null

Here is the code of one of the four buttons.

        buttonChoose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showFileChooser("pic");
                }
            });
    
       private void showFileChooser(final String flg) {
            Intent intent = new Intent();
            intent.putExtra("flg", flg);
            setResult(Activity.RESULT_OK, intent);
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
                filePath = data.getData();
                Log.d(TAG, "filePath 1 >>" + filePath);
    
                try {
                    bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                    String myTag = data.getStringExtra("flg");
    
                    Log.d(TAG, "myTag >>" + myTag);
    
                    if (myTag.equalsIgnoreCase("pic")) {
                        imageView.setImageBitmap(bitmap);
                    } else if (myTag.equalsIgnoreCase("License")) {
                        imageViewLicense.setImageBitmap(bitmap);
                    } else if (myTag.equalsIgnoreCase("RoadWorthiness")) {
                        imageViewRoadWorthiness.setImageBitmap(bitmap);
                    } else if (myTag.equalsIgnoreCase("Insurance")) {
                        imageViewInsurance.setImageBitmap(bitmap);
                    } else {
    
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
````````

The brief answer: you can't do that

The reason: Intents are used in single purpose one-way trip; you can't use a single intent in a round-trip, and especially if one of them is triggered by the system.

In your example, the intent that you used to open the FileChooser activity component is not the same as the returned back on onActivityResult() . The later one is not derived by the developer, but it's just a new different intent created by android system upon your action interacting with the FileChooser; if you really choose a file, then it returns an intent customized with data (with a result of RESULT_OK ). and if you choose nothing it returns null with a result of ( RESULT_CANCELED ).

This leads us that using setResult(Activity.RESULT_OK, intent); in your code doesn't matter, because android system will change it depending on whether the user selects a file or not regardless the incoming intent that you used to open the FileChooser.

For more info: https://developer.android.com/training/basics/intents/result

You can achieve what you want by tracking the state with a flag or utilizing the requestCode of the onActivityResult() and set the flag/requestCode with a different value for each button click

Solution with requestCode

private static final int PIC = 100;
private static final int LICENSE = 101;
private static final int ROAD_WORTHINESS = 102;
private static final int INSURANCE = 103;


buttonChoose.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        showFileChooser(PIC);
    }
});

buttonChoose2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        showFileChooser(LICENSE);
    }
});

private void showFileChooser(int requestCode) {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), requestCode);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && data != null && data.getData() != null) {
        filePath = data.getData();
        Log.d(TAG, "filePath 1 >>" + filePath);

        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            String myTag = data.getStringExtra("flg");

            Log.d(TAG, "myTag >>" + myTag);

            switch(requestCode) {
                case PIC:
                    imageView.setImageBitmap(bitmap);
                    break;
                case LICENSE:
                    imageViewLicense.setImageBitmap(bitmap);
                    break;
                case ROAD_WORTHINESS:
                    imageViewRoadWorthiness.setImageBitmap(bitmap);
                    break;
                case INSURANCE:
                    imageViewInsurance.setImageBitmap(bitmap);
                    break;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
````````

Solution with a flag

private static final int PIC = 100;
private static final int LICENSE = 101;
private static final int ROAD_WORTHINESS = 102;
private static final int INSURANCE = 103;
private int mState;

buttonChoose.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mState = PIC;
        showFileChooser();
    }
});

buttonChoose2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mState = LICENSE;
        showFileChooser();
    }
});

private void showFileChooser() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
        filePath = data.getData();
        Log.d(TAG, "filePath 1 >>" + filePath);

        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            String myTag = data.getStringExtra("flg");

            Log.d(TAG, "myTag >>" + myTag);

            switch(mState) {
                case PIC:
                    imageView.setImageBitmap(bitmap);
                    break;
                case LICENSE:
                    imageViewLicense.setImageBitmap(bitmap);
                    break;
                case ROAD_WORTHINESS:
                    imageViewRoadWorthiness.setImageBitmap(bitmap);
                    break;
                case INSURANCE:
                    imageViewInsurance.setImageBitmap(bitmap);
                    break;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
````````

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