The Android system itself can easily implement the sharing function, because we only need to pass an ACTION_SEND Intent to startActivity, and the system will pop up an application list for us. In fact, in the system's file manager, this should be a common function we use (including opening the file Intent.ACTION_VIEW).
Here is a simple way to share
Intent shareIntent = new Intent(); (Intent.ACTION_SEND); (Intent.EXTRA_TEXT, "This is my text to send."); ("text/plain"); startActivity(sendIntent);
Needless to say, the first two lines of code are just a simple Action Intent, the third line Intent.EXTRA_TEXT is a text type, as well as EXTRA_EMAIL, EXTRA_CC, EXTRA_BCC, EXTRA_SUBJECT, etc. These can be understood by the literal meaning.
Let's focus on Intent.EXTRA_STREAM.
Set the appropriate MIME type and place a URI pointing to the data in the EXTRA_STREAM in the attachment data to share the binary data. This is usually used to share pictures, but it can also be used to share any type of binary content, such as videos, files, etc.
Intent shareIntent = newIntent(); (Intent.ACTION_SEND); (Intent.EXTRA_STREAM, uriToImage); ("image/jpeg"); startActivity((shareIntent, getResources().getText(.send_to)));
Let’s talk about this method below:
There are many types of parameters, just list a few, "text/plain", "image/jpeg", "audio/mp4a-latm", "audio/x-mpeg", "video/mp4" and many more...
Here is a method to obtain the type
/** * Obtain the corresponding MIME type according to the file suffix name. * @param filePath */ public static String getMimeType(String filePath) { MediaMetadataRetriever mmr = new MediaMetadataRetriever(); String mime = "text/plain"; if (filePath != null) { try { (filePath); mime = (MediaMetadataRetriever.METADATA_KEY_MIMETYPE); } catch (IllegalStateException e) { return mime; } catch (IllegalArgumentException e) { return mime; } catch (RuntimeException e) { return mime; } } return mime; }
We can send multiple contents at the same time, and to send multiple data, use ACTION_SNED_MULTIPLE and a URI list pointing to the data. The MIME type varies according to the content shared. For example, if you share 3 JPEG images, the type is "image/jpeg". If there are different image types, then "image/*" should be used to match the activities that process different types of images. If you want to deal with various types, you should use "*/*". As mentioned earlier, analyzing and processing sharing is the matter of data receiving programs.
But it should be clear that ** must ensure that the data pointed to by the URI must be accessible to ** by the receiving program.
Another knowledge point is that we can filter the shared apps. For example, I only want to share them on QQ and WeChat platforms, and I don’t care about apps like Renren and Thunder
Can be achieved through methods,
First we define an Action Intent
String type = getMimeType(path); Intent shareIntent = new Intent(); (Intent.ACTION_SEND); (Intent.EXTRA_STREAM,(file)); (Intent.FLAG_ACTIVITY_NEW_TASK); (Intent.FLAG_GRANT_READ_URI_PERMISSION); (getMimeType(path));
Then get the ResolveInfo list that can be used to send this type of file, that is, the application list information that can send this type of file.
List<ResolveInfo> resInfo = ().queryIntentActivities(shareIntent, 0);
Filter out the application we want by package name
ArrayList<Intent> targetIntents = new ArrayList<Intent>(); for (ResolveInfo info : resInfo) { ActivityInfo activityInfo = ; if (("") ||("")) { Intent intent = new Intent(Intent.ACTION_SEND); (); (Intent.EXTRA_STREAM,(file)); (, ); (intent); } }
Finally use open
Intent chooser = ((0), "Send mail..."); (Intent.EXTRA_INITIAL_INTENTS, (new Parcelable[]{})); (chooser);
At this point, using Intent.ACTION_SEND to share is almost finished. Is it much more convenient than applying for Youmeng and various platforms...
Attach the source code for backup
/** * Send files * @param context * @param path */ public static void sendFileByOtherApp(Context context, String path) { File file = new File(path); if (()) { String type = getMimeType(path); Intent shareIntent = new Intent(); (Intent.ACTION_SEND); (Intent.EXTRA_STREAM,(file)); (Intent.FLAG_ACTIVITY_NEW_TASK); (Intent.FLAG_GRANT_READ_URI_PERMISSION); (getMimeType(path)); List<ResolveInfo> resInfo = ().queryIntentActivities(shareIntent, 0); if (!()) { ArrayList<Intent> targetIntents = new ArrayList<Intent>(); for (ResolveInfo info : resInfo) { ActivityInfo activityInfo = ; if (("") ||("")) { Intent intent = new Intent(Intent.ACTION_SEND); (); (Intent.EXTRA_STREAM,(file)); (, ); (intent); } } Intent chooser = ((0), "Send mail..."); (Intent.EXTRA_INITIAL_INTENTS, (new Parcelable[]{})); (chooser); } } }
Of course, we can also make an Activity, like QQ WeChat, to receive files or wen z, as long as we know how to use intentfilter
<intent-filter> <action android:name="" /> <category android:name="" /> <data android:mimeType="image/*" /> </intent-filter> <intent-filter> <action android:name="" /> <category android:name="" /> <data android:mimeType="text/plain" /> </intent-filter> <intent-filter> <action android:name=".SEND_MULTIPLE" /> <category android:name="" /> <data android:mimeType="image/*" /> </intent-filter>
Then in the oncreate method
if (Intent.ACTION_SEND.equals(action) && type != null) { if ("text/plain".equals(type)) { handleSendText(intent); // Process the sent text } else if (("image/")) { handleSendImage(intent); // Process the sent pictures } } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) { if (("image/")) { handleSendMultipleImages(intent); // Process multiple pictures sent } } else { // Handle other intents, such as starting from the home screen }
This way, the basic usage is introduced. The basic sharing functions can be almost completed, but if you want to share the graphic content or customize the sharing interface, you may need to dig deeper.
The above is the entire content of this article. I hope that the content of this article has a certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.