I want to get result from sharing a file on Android

744 views Asked by At

I have to share a url on android default but i want a response if url is being shared or just intent is closed. I am using on activity result but when i share with gmail it return 0(CANCELED) same when i close the intent.I need this to set text shared on a text view.Here is my code.

public void executeShareLinkClick() {
   Intent intentShare = new Intent(ACTION_SEND);
    intentShare.setType("text/plain");
    viewModel.isLinkSharedOpen.set(true);
    intentShare.putExtra(Intent.EXTRA_TEXT,"My Text of the message goes here ... write anything what you want");
    startActivityForResult(Intent.createChooser(intentShare, "Shared the text ..."),111);
}

public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
   if (resultCode==RESULT_OK){
       viewModel.isLinkShared.set(true);
   }else{
       viewModel.isLinkShared.set(false);
   }
    viewModel.isLinkSharedOpen.set(false);
}
2

There are 2 answers

2
CommonsWare On BEST ANSWER

Sorry, but what you want is not an option. ACTION_SEND does not support a result, and apps do not have to indicate whether or not the user sent your content.

The closest thing you can do is to add EXTRA_CHOSEN_COMPONENT_INTENT_SENDER to the Intent returned by createChooser(). Through that, you can find out if the user chose something in that chooser. However:

  • I don't think you find out if the user chose nothing

  • It still does not indicate that the user actually used the chosen app to send your content

0
TomInCode On
if(getIntent().getData() != null) {
    Uri fileUri = getIntent().getData();
    File file = new File(fileUri);

}

// for multiple files
if(getIntent().getAction().equals(Intent.ACTION_SEND_MULTIPLE) && getIntent().getClipData() != null) {

    int count = getIntent().getClipData().getItemCount();
    int currentUri = 0;
    while(currentUri < count) {
        Uri fileUri = getIntent().getClipData().getItemAt(currentUri).getUri();
        File file = new File(fileUri);
        currentUri = currentUri + 1;
    }
}