I'm trying to share custom data using the Sharesheet and the first question is: is it possible?
Example: In my app there is an address book and I want to share with a friend via Whatsapp a single contact [class: Contact]. When he receive the message he should be able to open the contact directly in the app or, if it's not installed, he should be directed to the play store to download the app.
I found how to send simple text and I've build for each class I want to share a corresponding Parcelable. Now the following is my code, the Sharesheet is opening but if I select the contact I want to share with it give me an error message: "Sharing failed, retry" (Whatsapp) or "Unsupported content" (Telegram).
sContact = new Contact();
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
ArrayList<Contact> ContactsList = new ArrayList<Contact>();
ContactsList.add(sContact);
Parcel.obtain().readList(ContactsList, Contact.class.getClassLoader());
sendIntent.putParcelableArrayListExtra("ShareContact", ContactsList);
sendIntent.setType("image/*");
Intent shareIntent = Intent.createChooser(sendIntent, null);
startActivity(shareIntent);
Another thing I can't understand is that if I add an item to the ContactsList the app crashes with error "android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.myapp.Contact", instead with the list empty the sharesheet opens.
is it possible?
In terms of what you mean by "custom data", no.
When he receive the message he should be able to open the contact directly in the app or, if it's not installed, he should be directed to the play store to download the app.
You could share an https
URL to your Web server, where the URL contains an opaque identifier of the contact. You could have your app support a deep link for that URL structure, such that if the user has the app installed, clicking the link might open your app (where you can decode the URL and use the identifier). Your Web server could do whatever it wants for the actual page, including issuing a redirect to your app's market:
URL
if the Web server thinks that the requesting client is an Android Web browser.
Now the following is my code, the Sharesheet is opening but if I select the contact I want to share with it give me an error message
First, the other apps do not have your Contact
class. If you put one in your Intent
, they will crash with a ClassNotFoundException
.
Second, you are not following the rules for ACTION_SEND
. When other apps attempt to use your Intent
, even if the Contact
was not a problem, they would fail because they will try to use EXTRA_TEXT
or EXTRA_STREAM
, and you do not have one. And they will attempt to use image/*
as a MIME type, which not only fails to match your non-existent text or stream, but it also is a wildcard, and you need to provide a concrete MIME type.