- Sharing Content between Android apps
- Sharing text
- Sharing HTML text
- Receiving text
- Sharing files and images
- Receiving files
- The Support Library is your friend
- How to Share and Send Any File on Android via WhatsApp and SMS
- Ashish Mundhra
- CloudSend for Android
- Conclusion
- Read Next
- 8 Best Fixes for Windows 10 Task Manager Not Working
- Top 7 Ways to Fix File Explorer Not Working on Windows 11
- Top 6 Ways to Fix Widgets Not Working on Windows 11
- Top 6 Ways to Fix Windows 11 Taskbar Not Working
- Top 9 Ways to Fix Windows 10 Search Not Working
- Top 8 Fixes for Action Center Not Opening in Windows 10
- How to Fix Windows Explorer Not Responding in Windows 10
- Top 8 Ways to Fix File Explorer Not Opening on Windows 10
- Did You Know
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.
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.
Источник
How to Share and Send Any File on Android via WhatsApp and SMS
Ashish Mundhra
01 Jan 2013
WhatsApp is one of the best ways to transfer files between two smartphones over the internet. However it has certain limitations which could be frustrating at times.
- Only audio, image or a video files can be transferred to the recipient. There is no provision to share any other file from the Android file system like a Word or a PDF document.
- WhatsApp compresses the image resolution before sharing the file. The image will look fine when viewing on the device, but when you view it on a computer you will notice the quality of the image to be significantly downgraded.
- The maximum size of the video that can be transferred using WhatsApp is 12 MB. We have seen some tricks in the past using which we can compress and trim an HD video but that reduces the original quality too. Moreover, splitting a video file on the device is not an easy task.
CloudSend is an Android app that can help you counter the above problems. The app requires a Dropbox account and therefore it’s necessary that you have one before continuing any further. CloudSend can transfer any file on your device to anyone using SMS or WhatsApp with Dropbox as the intermediary.
CloudSend for Android
Before you download CloudSend on your Android device, download and install the official Dropbox app for Android if you don’t already have it. Open the Dropbox app and log in to your account. Having done that, you can go ahead and install CloudSend.
When you launch CloudSend, it will give you a quick overview of the app. Swipe your screen from right to left to open the Dropbox authorization page. Here you will have to grant CloudSend access over your Dropbox account but unlike other apps which ask access over all the folders that are in your account, this app creates a separate CloudSend folder for the task and just asks complete control over it. CloudSend will use the Dropbox app installed on your device to ask for authorization and once it’s done, you can go ahead and close it.
Now navigate to the file you want to transfer. If you would like to transfer a file from the gallery, open the app. However, if the file you want to transfer is a non-gallery item (like documents, archive, etc.) you can open your Android file manager. Long-tap on the file you wish to transfer and select Share.
Select CloudSend as your sharing app and wait for the app to upload the file to your Dropbox account. The app has no ads and doesn’t come with any upload limitation. The upload time will depend upon the size of the file you are transferring and your connection speed. When the file has been uploaded successfully, the app will give you a notification in the drawer.
You can now tap the notification and wait for the program to generate a sharing link. This link can now be shared via WhatsApp, email, SMS and any other text sharing app you have installed on your Android device. The recipient will receive the link from where he can preview (if supported) and download the file using the browser.
Cool Tip: Send It is a similar app for Android which uses Google Drive instead of Dropbox for the aforementioned task.
Conclusion
So that was how you can transfer any file from Android to any other smartphone or even a computer. The app simply reduces the effort you would have to put manually in uploading the files to Dropbox, generating the link and then sharing it with your friend. Should do a good bit in enhancing your productivity, especially if you are on WhatsApp every day as much as I am.
Last updated on 8 Feb, 2018
The above article may contain affiliate links which help support Guiding Tech. However, it does not affect our editorial integrity. The content remains unbiased and authentic.
Read Next
8 Best Fixes for Windows 10 Task Manager Not Working
Is # Windows 10 # Task Manager not working or opening? Here are 8 ways to solve this error including a third-party software to replace the Task Manager.
Top 7 Ways to Fix File Explorer Not Working on Windows 11
# File Explorer not working on # Windows 11? Check out these # troubleshooting fixes to resolve the issue.
Top 6 Ways to Fix Widgets Not Working on Windows 11
Unable to use # Widgets on # Windows 11? Apply these fixes to resolve the # Widgets-related issues on # Windows 11.
Top 6 Ways to Fix Windows 11 Taskbar Not Working
Are you having problems with # Windows 11 # taskbar? Here’s how you can troubleshoot the taskbar not working in # Windows 11.
Top 9 Ways to Fix Windows 10 Search Not Working
Are you unable to use and type on # Windows # Search on your computer? Check out 9 ways to fix # Windows 10 Search bar not working issue.
Top 8 Fixes for Action Center Not Opening in Windows 10
Are you unable to open Action Center on your # Windows 10 PC? Check out 10 ways to fix the problem of Action Center won’t open on # Windows 10 computer.
How to Fix Windows Explorer Not Responding in Windows 10
Does your # Windows File Explorer crash or fail to respond at the drop of a hat? If yes, you might want to try the following fixes of # Windows Explorer not responding.
Top 8 Ways to Fix File Explorer Not Opening on Windows 10
Is the # File Explorer app giving you troubles on # Windows 10? Read the post to learn how to fix # File Explorer not opening or not responding on # Windows 10.
Did You Know
India is one of the largest active user bases on WhatsApp.
Источник