Any share android application

Sharing Content between Android apps

Sharing is caring, as they say, but sharing on Android means something perhaps slightly different. ‘Sharing’ is really shorthand for sending content such as text, formatted text, files, or images between apps.

So if ‘sharing’ == sending content, it makes slightly more sense that it is implemented using ACTION_SEND (or ACTION_SEND_MULTIPLE) Intents and its dozen extras.

While that approach is perfectly valid, I prefer to use ShareCompat, a set of classes in the v4 Support Library designed to make it easy to build intents for sharing content.

Sharing text

Sharing plain text is, as you might imagine, a good place to start. In fact, there’s not a whole lot to it:

ShareCompat.IntentBuilder uses a fluent API where you can chain together multiple method calls, using only the ones you need. For sharing, one of the most important parts is picking the right mime type — this is how apps filter what type of content they can receive. By using text/plain, we signify that our Intent will only contain plain text. Then, of course, setText() is how we actually add the CharSequence to the Intent to send. And while you can certainly send styled text using setText(), there’s no guarantee that the receiving app will honor that styling, so you should ensure that the text is legible with or without styling.

You’ll note we then use resolveActivity() before calling startActivity(). As mentioned in Protecting Implicit Intents with Runtime Checks, this is critical to prevent an ActivityNotFoundException when there is no Activity available to handle the mime type you have selected. While probably not as much of a concern with text/plain, it may be much more common with other types.

Note: when you use startActivity(shareIntent), that respects any default apps the user has set (i.e., if they’ve previously selected sharing all “text/plain” items to a certain app). If you’d like to instead always show a disambiguation chooser, use the intent generated from IntentBuilder.createChooserIntent() as explained in the ACTION_CHOOSER documentation.

Sharing HTML text

Some apps, most notably email clients, also support formatting with HTML. The changes, compared to plain text, are fairly minor:

The differences here are that we use of setHtmlText() in place of setText() and a mime type of text/html replacing text/plain. Here ShareCompat actually does a little bit extra: setHtmlText() also uses Html.fromHtml() to create a fallback formatted text to pass along to the receiving app if you haven’t previously called setText() yourself.

Given that many of the apps that can receive HTML text are email clients, there’s a number of helper methods to set the subject, to:, cc:, and bcc: email addresses as well — consider adding at least a subject to any share intent for best compatibility with email apps.

Of course, you’ll still want to call resolveActivity() just as before — nothing changes there.

Receiving text

While the focus so far has been on the sending side, it is helpful to know exactly what is happening on the other side (if not just to build a simple receiving app to install on your emulator for testing purposes). Receiving Activities add an intent filter to the Activity:

Читайте также:  Как узнать здоровье батареи андроид

The action is obviously the more critical part — without that there’s nothing that would denote this as an ACTION_SEND (the action behind sharing). The mime type, same as with our sending code, is also present here. What isn’t as obvious are the two categories. From the element documentation:

Note: In order to receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter. The methods startActivity() and startActivityForResult() treat all intents as if they declared the CATEGORY_DEFAULT category. If you do not declare it in your intent filter, no implicit intents will resolve to your activity.

So CATEGORY_DEFAULT is required for our use case. Then, CATEGORY_BROWSABLE allows web pages to natively share into apps without any extra effort required on the receiving side.

And to actually extract the information from the Intent, the useful ShareCompat.IntentReader can be used:

Similar to IntentBuilder, IntentReader is just a simple wrapper that make it easy to extract information.

Sharing files and images

While sending and receiving text is straightforward enough (create text, include it in Intent), sending files (and particularly images — the most common type by far) has an additional wrinkle: file permissions.

The simplest code you might try might look like

And that almost works — the tricky part is in getting a Uri to the File that other apps can actually read, particularly when it comes to Android 6.0 Marshmallow devices and runtime permissions (which include the now dangerous READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions).

My plea: don’t use Uri.fromFile(). It forces receiving apps to have the READ_EXTERNAL_STORAGE permission, won’t work at all if you are trying to share across users, and prior to KitKat, would require your app to have WRITE_EXTERNAL_STORAGE. And really important share targets, like Gmail, won’t have the READ_EXTERNAL_STORAGE permission — so it’ll just fail.

Instead, you can use URI permissions to grant other apps access to specific Uris. While URI permissions don’t work on file:// URIs as is generated by Uri.fromFile(), they do work on Uris associated with Content Providers. Rather than implement your own just for this, you can and should use FileProvider as explained in the File Sharing Training.

Once you have it set up, our code becomes:

Using FileProvider.getUriForFile(), you’ll get a Uri actually suitable for sending to another app — they’ll be able to read it without any storage permissions — instead, you are specifically granting them read permission with FLAG_GRANT_READ_URI_PERMISSION.

Note: we don’t call setType() anywhere when building our ShareCompat (even though in the video I did set it). As explained in the setDataAndType() Javadoc, the type is automatically inferred from the data URI using getContentResolver().getType(uriToImage). Since FileProvider returns the correct mime type automatically, we don’t need to manually specify a mime type at all.

If you’re interested in learning more about avoiding the storage permission, consider watching my Forget the Storage Permission talk or at least go through the slides, which covers this topic in depth at 14:55 (slide 11).

Receiving files

Receiving files isn’t too different from text because you’re still going to use ShareCompat.IntentReader. For example, to make a Bitmap out of an incoming file, it would look like:

Of course, you’re free to do whatever you want with the InputStream — watch out for images that are so large you hit an OutOfMemoryException. All of the things you know about loading Bitmaps still apply.

Читайте также:  Panda cms для андроид

The Support Library is your friend

With both ShareCompat (and its IntentBuilder and IntentReader) and FileProvider in the v4 Support Library, you’ll be able to include sharing text, HTML text, and files in your app with the best practices by default.

Источник

Android SDK: Implement a Share Intent

This tutorial runs through the basic process of creating a share button, implementing the share Intent, passing your content, and building the chooser list.

Implementing the share Intent allows users of your apps to share content across multiple channels, including email, text messaging, social networking and more. You can give your users a good level of control over how they wish to share your content by letting them select from the list of sharing applications available on their own devices.

Step 1: Start a New Android Project

If you already have an application you want to implement sharing with, you can use it. If not, create a new project in your Android IDE. In Eclipse, choose «File,» «New,» «Project» then «Android Project.» Fill in your project details and click «Finish.» your new app’s details will appear in the workspace.

Step 2: Choose an Activity to Launch the Share Intent

If you already have an Activity in your Android app that you plan on launching the share Intent from, open it in your IDE’s editor area. Otherwise, you can use the main class for your new app or create a new class, making it an Activity so that you can include a button to launch your sharing Intent. Make your class extend the Activity class and include an «onCreate» method in which you can build your user interface elements.

Step 3: Create a Share Button

You can launch your sharing Intent on any user action, such as pressing an options or context menu item. The following Java code creates an image button, inside the «onCreate» method of the Activity:

This creates a button based on the Android ImageButton class, which the Activity will need to import. This code refers to an image saved with the file-name «sharing» which is stored the drawable folder of the application resources. You can use your own image and alter the code to reflect its name. Alternatively, you can create a standard button with text rather than an image.

Step 4: Listen for Button Clicks

Depending on which type of button you’re using to launch your Intent, you may need to implement a listener function. For the image button, you can add the following code:

This code specifies a method for the application to call when users press the share button. By including the code in a dedicated method, you can call on the sharing functionality from multiple locations in your code, such as from the «onContextItemSelected» method for long-presses or the «onOptionsItemSelected» for option menu button presses.

Step 5: Implement the Share Method

Add a new method to your Activity, matching the name specified in your button listener method, as follows:

This method will contain the implementation code for sharing content from your app. You can choose to pass parameters to the method if this suits your project.

Step 6: Create a Send Intent

Create the sharing Intent. Add the following Java code inside your sharing method, creating an Intent object with the send action type:

Читайте также:  Png to jpeg android

Step 7: Set the Sharing Type

Set a MIME type for the content you’re sharing. This will determine which applications the chooser list presents to your users. Plain text, HTML, images and videos are among the common types to share. The following Java code demonstrates sending plain text:

This is a flexible option, as you can send plain text reliably through many different channels.

It is possible to target specific applications using the «setType» method, but this can be a risky strategy, potentially causing problems if the user does not have those particular apps installed. By keeping the sharing function as generic as possible, you give your users control over how they want to share your content. Sticking to the standard behaviour for sharing in Android applications also creates an intuitive user experience.

Step 8: Build the Share Content

You can pass various elements of your sharing content to the send Intent, including subject, text / media content, and addresses to copy to in the case of email sharing. This Java code builds a string variable to hold the body of the text content to share:

You can of course build the content using variables and methods within your application.

Remember that some of your content will not appear when the user chooses certain channels. For example, if you set a subject, it will not appear if the user chooses to share using text messaging, and anything over 140 characters will be cropped if the user chooses Twitter.

Step 9: Pass Content to the Intent

Pass your sharing content to the «putExtra» method of the Intent class using the following Java code:

This code first adds a subject to the sharing content, then adds the text body by referring to the string variable.

Step 10: Create a Chooser

Now that you have defined the content to share when the user presses the share button, you simply have to instruct Android to let the user choose their sharing medium. Add the following code inside your share method:

This code passes the name of the sharing Intent along with a title to display at the top of the chooser list. This example uses «Share via» which is a standard option you may have seen in existing apps. However, you can choose a title to suit your own application.

When the user chooses an application from the list, your share content will be passed to that application. For example, if the user chooses an email application, any subject you specified will be automatically populated in the subject field. The user will be able to edit the content before sending it if they wish to do so.

Conclusion

The process of implementing a share Intent in your Android applications is not a complex one. However, the bigger challenge is choosing your sharing content in a way that serves the purpose of your application, while keeping it user-friendly. For example, you can’t share the same content in a text message or tweet that you could send using email. For this reason it’s best to keep your sharing content as general as possible, so that the function will be as effective for Twitter and Facebook as it is for Gmail and email.

If you want to improve as an Android developer, check out the range of useful Android app templates on Envato Market. There’s sure to be something there that can speed up your next project.

Источник

Оцените статью