- 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 Files Between Android and Windows on Your Network
- Share Android Files with Windows
- Your Phone App
- Sweech App
- How to Share Windows Files with Android
- Making a File or Folder Public
- Accessing the Files on Android
- Never Miss Out
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 Files Between Android and Windows on Your Network
With smartphones becoming a near-essential part of everyday life, you may find yourself in moments where you want to transfer files between an Android device and your Windows machine. Fortunately, it’s very easy to achieve this these days with a number of good third-party apps (as well as onboard Windows methods, to an extent) here to help.
Here we cover the best ways to move files from Android to Windows and vice versa.
Also read: How to Share Files Between Android and Ubuntu on Your Network
Share Android Files with Windows
It’s worth noting that there are plenty of ways to perform this transfer. Bluetooth is useful, as is plugging in a cable between your PC and your phone. However, this tutorial focuses on how to transfer over your Wi-Fi network. Even with these restrictions, the competition is pretty tight. We’re focusing on two apps to transfer files between Android and Windows 10: Your Phone and Sweech.
Your Phone App
For a couple of years now, Windows has had the “Phone companion” feature. It used to be pretty threadbare and still doesn’t just give you full access to everything that’s on your phone, but certainly does the job of managing and moving Android photos through Windows 10, as well as reading, sending and deleting messages.
On your Android device, you need to get the “Your Phone Companion” app, while on Windows, the “Your Phone” app should already be on your PC. You can find it by searching for “your phone” through a Start menu search. (If it’s not there, you can download Your Phone from the Microsoft Store.)
Once you have both of these apps on their respective devices, open “Your Phone” on your PC, making sure that your phone and PC are connected to the same network.
Follow the instructions to connect your devices, then once you’re in the app, click the “Photos” icon in the pane on the left to look at all the images on your phone.
From here, you can copy-paste images to your PC, open them in a desktop app, or even drag and drop them straight onto your desktop! Simple.
Sweech App
The other app we’re focusing on is Sweech, because it’s very quick to set up; you can be transferring files within one to two minutes of downloading it!
When you download Sweech, press the large play button that appears and give the app permission to access your files. Sweech will show you a local IP address and port.
Type it into the web browser on your PC, hit Enter, and you’ll see your phone’s file system.
You can browse your phone as if it were a folder on your PC. This includes dragging files from your phone onto your PC, as well as putting files into folders on your phone. Once you’re done, tap the Stop button on the app to shut off the file transfer.
Every time you use Sweech, it will use the same IP address and port. As such, if you don’t want to keep typing in the address when you use Sweech, you can bookmark the page for quick access.
You can use the shortcuts on the left to filter content by type. You can even send data to your phone’s clipboard from your PC. Just type what you want on it, then click the blue clipboard icon to transfer it to the phone.
Also read: How to Install Google Play Store on Windows 11
How to Share Windows Files with Android
Accessing Windows folders on Android is a little trickier. First, you need to make the file or folder accessible by outside devices.
Making a File or Folder Public
The easy way to do this is to move or copy the files over to your Windows Public folder. Typically, this is located at “C:\Users\Public”. This folder is special because it’s automatically set up to allow other users on your network to access it.
If you want to share a folder without moving it to Public, you can set it up as if it were in the Public folder. To do this, right-click the file or folder you want to share, then click Properties.
Go to the Sharing tab, then click Share.
On the drop-down menu, select “Everyone,” then click Add. Click on Share at the bottom right.
By default, you’ll need your user credentials to access your PC’s public folders from outside. If you’d rather not enter them every time and trust all the devices on your network, you can instead disable the requirement and allow guests to see your public files.
To do this, type “advanced sharing” in the Start menu and select the option that appears.
Scroll down to the “All Networks” category – you may need to expand it. In this category, find “Turn off password-protected sharing” and click it, then click Save Changes.
Now you can access your public files without logging in. Be sure to turn this back on if you don’t want others on your network snooping on your files!
Also read: How to View Shared Files and Folders in Windows 10
Accessing the Files on Android
Now it’s time to get access to those files. You’ll need a file explorer that has LAN capabilities so that it can access your PC.
If you have an app of choice, use that. For this article, however, we are using FE File Explorer. We chose this app because it makes it easy to connect to your PC, whether you’re using an account or a guest login.
When you boot up FE File Explorer, tap the Plus button at the bottom right.
Here, you can enter the network details of your PC. If you’re on the same Wi-Fi network as your PC, you need not bother – just scroll to the bottom and find your PC in the neighborhood section.
The app will ask you if you want to log in to your PC or use a guest account. If you kept password-protected sharing on, select the top option and log in. If you turned it off, select the guest option.
Once done, you should now be able to navigate your PC’s public folders. Navigate to the files you want to download, then either tap the three dots next to a single file to see all the actions you can perform or hold-press a file to select multiple files to download or move.
As you can see, it is quite easy to transfer files between Android and Windows 10. If you are using Mac, you can make use of OpenMTP to transfer files from Android to Mac. You can even transfer files from your Android phone to your Smart TV.
Also read: How to Connect Two Computers Using a LAN Cable in Windows 10
Remember, there’s a lot more you can do if you have a Windows PC and an Android phone. For starters, you can send text messages from your desktop, run Android on your PC, and also install Windows 10 from Android.
Never Miss Out
Receive updates of our latest tutorials.
Content Manager at Make Tech Easier. Enjoys Android, Windows, and tinkering with retro console emulation to breaking point.
Источник