user7997603
user7997603

Reputation: 21

Share intent not working in android marshmallow

I am using the below code for share content in android app, the code is working properly for android 7.+ but not working for marshmallow(6.0.1). please suggest.

shareNow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
           Intent sharingIntent = new Intent(Intent.ACTION_SEND);
           sharingIntent.setType("text/plain");
           String shareBody = Constants.SHARE_TEXT + refer_code;
           sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Share");
           sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
           startActivity(Intent.createChooser(sharingIntent, "Share via"));
            } catch (Exception e) {
                ShowToastMsg.showToast(getActivity(), "Error Occured");
            }
        }
    });

Upvotes: 1

Views: 1001

Answers (2)

Pratik Butani
Pratik Butani

Reputation: 62419

I got this problem once,

I have added flag FLAG_ACTIVITY_NEW_TASK and that worked fine.

Hope it will be useful to you.

Note: I got that error after signing apk, so may you have to check after sign your apk.

Upvotes: 0

vikas kumar
vikas kumar

Reputation: 11018

It may be due to the runtime permission. Android marshmallow requires runtime permission for access the image files stored in the device. So you need to give runtime permission in code.

so make sure you are giving the required premissions like READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE so be careful before making request. And if you dont want to handle the permissions part here is way to do that.

// Provide read access
shareIntent.setData(uriToImage);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

there is a detailed guide how to use new features. Link

Please visit https://developer.android.com/training/permissions/requesting.html

Note : You can also use the direct share here is tutorial.

Upvotes: 2

Related Questions