Passing Multiple text/plain values in share intent

2019-07-28 21:20发布

问题:

I am creating a notepad application in android. I have given a functionality to share individual note with other apps. I want the share function to share the title and content of the note. I can't seem to make it work. Below is my java code for share intent.

JAVA

    String title=noteModel.getTitle();
    String content=noteModel.getContent();
    Intent intentShare = new Intent();
    intentShare.setAction(Intent.ACTION_SEND);

    intentShare.putExtra(Intent.EXTRA_TEXT,title);
    intentShare.putExtra(Intent.EXTRA_TEXT,content);            
    intentShare.setType("text/plain");
    context.startActivity(intentShare.createChooser(intentShare,"Send note to"));

回答1:

I found a way to pass multiple strings with just one EXTRA_TEXT. I wanted to pass two values, namely, title and content, So I stored the value of title in a string called "title" and the value of content in a string called "content". Now, the trick ! I concatenated both the strings together and stored that concatenated string to a new string and passed that string into EXTRA_TEXT.

I am gonna share my correct code just to give a clear picture.

String title=noteModel.getTitle();
String content=noteModel.getContent();
String titleAndContent="Title: "+title+"\n Content: "+content;

Intent intentShare = new Intent();
intentShare.setAction(Intent.ACTION_SEND);
intentShare.setType("text/plain");
intentShare.putExtra(Intent.EXTRA_TEXT,titleAndContent);

context.startActivity(intentShare);


回答2:

You used the same key for two Extra

    intentShare.putExtra(Intent.EXTRA_TEXT,title);
    intentShare.putExtra(Intent.EXTRA_TEXT,content);   

You have to use a different key for every extra you pass. Change the content extra similar to below one.

intentShare.putExtra(Intent.EXTRA_CONTENT,content);


回答3:

intentShare.putExtra(Intent.EXTRA_TEXT,title);
intentShare.putExtra(Intent.EXTRA_TEXT,content); ///ovewriting value

please use different key



回答4:

try this

intentShare.putExtra(android.content.Intent.EXTRA_SUBJECT, title);
intentShare.putExtra(android.content.Intent.EXTRA_TEXT, content);