{"id":1777,"date":"2017-01-31T09:50:45","date_gmt":"2017-01-31T09:50:45","guid":{"rendered":"http:\/\/www.niyati.com\/blog\/?p=1777"},"modified":"2018-08-14T11:50:44","modified_gmt":"2018-08-14T11:50:44","slug":"android-sharecompat","status":"publish","type":"post","link":"https:\/\/www.niyati.com\/blog\/android-sharecompat\/","title":{"rendered":"Sharing Content The Easy Way Using ShareCompat"},"content":{"rendered":"<p>If you ever <a href=\"https:\/\/www.niyati.com\/mobile-app-development.htm\" target=\"_blank\">worked on an app<\/a> that can share content with other <a title=\"Android App\" href=\"https:\/\/www.niyati.com\/android-app-development.htm\">apps<\/a>, you might have stumbled upon Intent.ACTION_SEND or Intent.ACTION_SEND_MULTIPLE. While this is good, there is an easy way to build intents and passing extras to share a content using ShareCompat.<\/p>\n<h3>ShareCompat<\/h3>\n<p dir=\"ltr\">The ShareCompat provide a couple of classes to easily build intents for sharing contents. It is available in v4 support library.<\/p>\n<p>The ShareCompat class contains two helper classes <strong>IntentBuilder<\/strong> and <strong>IntentReader<\/strong> used to share and receive contents respectively across apps.<\/p>\n<h3>Sharing plaintext using ShareCompat.IntentBuilder<\/h3>\n<p><div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/1d0ebed655e5ece601a3d39e8bb31f0b.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/1d0ebed655e5ece601a3d39e8bb31f0b\">Gist<\/a>.<\/noscript><\/div>\n<\/p>\n<p>Here we use IntentBuilder to build an intent with all needed extras for the content. The <code>setType()<\/code> method is used to set the mime type of the content. This is required to resolve apps that can handle this content.<\/p>\n<p>In the <code>setText()<\/code> method we can provide the content. This method takes CharSequence as parameter, so we can also style the text. However, the receiving app may or may not have support for displaying styled text.<\/p>\n<p>The <code>createChooserIntent()<\/code> method prompts the user with a chooser screen of all the applicable apps each time the content is shared. If we build intent without a chooser, then the default app the user has set for sharing this type of content is started automatically.<\/p>\n<h3>Sharing HTML content<\/h3>\n<p>You can also share HTML content. The <code>setHtmlText()<\/code> method is used to set html content to the intent extra. This method also initializes the <code>setText()<\/code> with the formatted text if it is not initialized before.<\/p>\n<p>If you want to send email, there are helper methods for adding a list of \u201cto\u201d, \u201ccc\u201d and \u201cbcc\u201d fields (<code>setEmailTo(), setEmailCc(), setEmailBcc()<\/code>). Use <code>setSubject()<\/code> to set the email\u2019s subject.<\/p>\n<h3>Sharing files<\/h3>\n<p>Here is an example to share an image.<\/p>\n<p><div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/965d6cb70ce1e8903aa48ad51f1d4073.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/965d6cb70ce1e8903aa48ad51f1d4073\">Gist<\/a>.<\/noscript><\/div>\n<\/p>\n<p>Here <code>setStream()<\/code> is used to set the Uri for the file you like to share. For example, to share an image set the type to \u201cimage\/png\u201d and set the Uri to <code>setStream()<\/code> method.<\/p>\n<p>You can also use <code>addStream()<\/code> to add multiple files. Then the intent created will become an ACTION_SEND_MULTIPLE intent.<\/p>\n<h3>Receiving content<\/h3>\n<p>Apps can receive a shared content using ShareCompat.IntentReader. This static class provides methods to read data and relevant information from the Intent.<\/p>\n<p>To receive content, you must declare an Intent Filter with the action and type of data your app can receive.<\/p>\n<p><div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/c690b4cb57ebb29f6322b25e3fe72296.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/c690b4cb57ebb29f6322b25e3fe72296\">Gist<\/a>.<\/noscript><\/div>\n<\/p>\n<p>Here action SEND is used to receive single content. If you wish to handle multiple contents, then use action SEND_MULTIPLE. The category DEFAULT is required to handle implicit intent. Category Browsable allows web pages to directly share content to the app.<\/p>\n<p>Specify the type of data your app can handle using the mimeType attribute.<\/p>\n<p>In the receiver class, get the intent and parse the content.<\/p>\n<p><div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/62c27213bac7b8357996e2d0c47c8c76.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/62c27213bac7b8357996e2d0c47c8c76\">Gist<\/a>.<\/noscript><\/div>\n<\/p>\n<p>We get the Intent using the IntentReceiver and check if it is a shared intent. If so, then start processing.<\/p>\n<p>For example to get a plaintext content,<\/p>\n<p><div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/bb6fe58134d5a73b464d2b9acfebe0c8.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/bb6fe58134d5a73b464d2b9acfebe0c8\">Gist<\/a>.<\/noscript><\/div>\n<\/p>\n<p>Similarly to get an image,<\/p>\n<p><div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/95da65ecbf41a428e3e11f095282d17a.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/95da65ecbf41a428e3e11f095282d17a\">Gist<\/a>.<\/noscript><\/div>\n<\/p>\n<p>You can find my demo on ShareCompat on <a href=\"https:\/\/github.com\/sjthn\/ShareCompatExample\" target=\"_blank\">my github page<\/a><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>If you ever worked on an app that can share content with other apps, you might have stumbled upon Intent.ACTION_SEND or Intent.ACTION_SEND_MULTIPLE. While this is good, there is an easy way to build intents and passing extras to share a content using ShareCompat. ShareCompat The ShareCompat provide a couple of classes to easily build intents [&hellip;]<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":8,"featured_media":2018,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,10],"tags":[],"class_list":["post-1777","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","category-mobile-app-development"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.niyati.com\/blog\/wp-json\/wp\/v2\/posts\/1777","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.niyati.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.niyati.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.niyati.com\/blog\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/www.niyati.com\/blog\/wp-json\/wp\/v2\/comments?post=1777"}],"version-history":[{"count":2,"href":"https:\/\/www.niyati.com\/blog\/wp-json\/wp\/v2\/posts\/1777\/revisions"}],"predecessor-version":[{"id":2060,"href":"https:\/\/www.niyati.com\/blog\/wp-json\/wp\/v2\/posts\/1777\/revisions\/2060"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.niyati.com\/blog\/wp-json\/wp\/v2\/media\/2018"}],"wp:attachment":[{"href":"https:\/\/www.niyati.com\/blog\/wp-json\/wp\/v2\/media?parent=1777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.niyati.com\/blog\/wp-json\/wp\/v2\/categories?post=1777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.niyati.com\/blog\/wp-json\/wp\/v2\/tags?post=1777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}