Content sharing between Android applications (2)

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/xiaanming/article/details/9428613

Content sharing between Android applications (1)


I have shared with you the applications you have developed and shared the content with other applications. For more information, click here. (1) today, we share content received from other applications. For example, if you develop a social network application, one of your activities can accept what people share from other applications, for example, share text or share images from the image library. Next we will use an example to explain how to accept content from other applications.

1. Create an android project named sharedcontext and modify the manifest file.

We need to define in the manifest file what kind of intent this activity can receive. We need to create an intent filter to use<intent-filter>Element to filter intent that we can receive. The following is a simple example. We believe that everyone knows the opposite. In the following example, our application can process text and text files, for a single image and multiple images, we define the manifest file as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. example. sharedcontext "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <uses-SDK Android: minsdkversion =" 8 "Android: targetsdkversion = "16"/> <application Android: allowbackup = "true" Android: icon = "@ drawable/ic_launcher" Android: Label = "@ string/app_name" Android: theme = "@ style/apptheme"> <activity Android: Name = "com. exa Mple. sharedcontext. mainactivity "Android: Label =" @ string/app_name "> <! -- Start intent of the application. In this example, there is only one activity --> <intent-filter> <action Android: Name = "android. intent. action. main "/> <category Android: Name =" android. intent. category. launcher "/> </intent-filter> <! -- To process the intent of text, we need to define action, category, and mime corresponding to the text --> <intent-filter> <action Android: Name = "android. intent. action. send "/> <category Android: Name =" android. intent. category. default "/> <data Android: mimetype =" text/* "/> </intent-filter> <! -- Process the intent of a single image --> <intent-filter> <action Android: Name = "android. intent. action. send "/> <category Android: Name =" android. intent. category. default "/> <data Android: mimetype =" image/* "/> </intent-filter> <! -- Process intent of multiple images --> <intent-filter> <action Android: Name = "android. intent. action. send_multiple "/> <category Android: Name =" android. intent. category. default "/> <data Android: mimetype =" image/* "/> </intent-filter> </activity> </application </manifest>

When an application builds an intent as above and passes it to startactivity (), our application will be listed in the intent selector, when the user selects the application and enters the corresponding activity (mainactivity in the above example), we only need to process the content in mainactivity and display it with the corresponding UI. mainactivity is as follows:

Package COM. example. sharedcontext; import Java. io. bytearrayoutputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. util. arraylist; import android. app. activity; import android. content. context; import android. content. intent; import android.net. uri; import android. OS. bundle; import android. view. view; import android. view. viewgroup; import android. widget. adapterview; import android. widget. A Dapterview. onitemclicklistener; import android. widget. baseadapter; import android. widget. gridview; import android. widget. imageview; import android. widget. textview; public class mainactivity extends activity {@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); intent = getintent (); // obtain the intent actionstring action = intent. getaction (); // get the intent MIME types Tring type = intent. GetType (); If (intent. action_send.equals (Action) & type! = NULL) {// here we process all text types if (type. startswith ("text/") {// process the obtained text. Here we use textview to display handlesendtext (intent);} // The MIME type of the image includes image/PNG, image/jepg, image/GIF, etc. Else if (type. startswith ("image/") {// process the obtained image. We use imageview to display handlesendimage (intent) ;}} else if (intent. action_send_multiple.equals (Action) & type! = NULL) {If (type. startswith ("image/") {// process multiple images. We use a gridview to display handlesendmultipleimages (intent );}}} /*** use textview to display text * to open a common text file * @ Param intent */private void handlesendtext (intent) {textview = new textview (this ); // for general text processing, we can directly display the string sharedtext = intent. getstringextra (intent. extra_text); If (sharedtext! = NULL) {textview. settext (sharedtext);} // process the text file, obtain the input stream from the URI, and then convert the input stream to the string URI texturi = (URI) intent. getparcelableextra (intent. extra_stream); If (texturi! = NULL) {try {inputstream = This. getcontentresolver (). openinputstream (texturi); textview. settext (inputstream2byte (inputstream);} catch (exception e) {e. printstacktrace () ;}/// set to activity setcontentview (textview );} /*** convert the input stream to a string * @ Param inputstream * @ return * @ throws ioexception */private string inputstream2byte (inputstream) throws ioexception {bytearrayoutputstream BOS = New bytearrayoutputstream (); byte [] buffer = new byte [1024]; int Len =-1; while (LEN = inputstream. Read (buffer ))! =-1) {Bos. write (buffer, 0, Len);} Bos. close (); // specify the encoding format as UIT-8return new string (Bos. tobytearray (), "UTF-8");}/*** use imageview to display a single image * @ Param intent */private void handlesendimage (intent) {URI imageuri = (URI) intent. getparcelableextra (intent. extra_stream); If (imageuri! = NULL) {imageview = new imageview (this); imageview. setimageuri (imageuri); setcontentview (imageview);}/*** use the gridview to display multiple images * @ Param intent */private void handlesendmultipleimages (intent) {final arraylist <URI> imageuris = intent. getparcelablearraylistextra (intent. extra_stream); If (imageuris! = NULL) {gridview = new gridview (this); // you can specify the item width in the gridview. setcolumnwidth (130); // The setting column automatically adapts to the gridview. setnumcolumns (gridview. auto_fit); gridview. setadapter (New gridadapter (this, imageuris); setcontentview (gridview); gridview. setonitemclicklistener (New onitemclicklistener () {@ overridepublic void onitemclick (adapterview <?> Parent, view, final int position, long ID) {// click the item in the gridview to share the picture to other applications. // here you can refer to the http://blog.csdn.net/xiaanming/article/details/9395991Intent intent = new intent (); intent. setaction (intent. action_send); intent. putextra (intent. extra_stream, imageuris. get (position); intent. settype ("image/*"); startactivity (intent. createchooser (intent, "shared image") ;}}}/ *** rewrite baseadapter * @ author xiaanming **/public class gridadapter extends baseadapter {private context mcontext; private arraylist <URI> list; Public gridadapter (context mcontext, arraylist <URI> List) {This. list = List; this. mcontext = mcontext;} @ overridepublic int getcount () {return list. size () ;}@ overridepublic object getitem (INT position) {return list. get (position) ;}@ overridepublic long getitemid (INT position) {return position ;}@ overridepublic view getview (INT position, view convertview, viewgroup parent) {imageview; if (convertview = NULL) {imageview = new imageview (mcontext); imageview. setpadding (8, 8, 8, 8);} else {imageview = (imageview) convertview;} imageview. setimageuri (list. get (position); Return imageview ;}}}

After running the program, select the system image library, select multiple images (1) for sharing, and share multiple interfaces for our own applications (2) Click the item of our application, choose to share a single image (3). We will continue to select our own application for display (4). Create a new memo to save and share it with you (5 ), display page of shared text files (6)





This is probably the content. If you go to bed and think this article is helpful to you, you can help me top it up. Thank you for your attention!


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.