Android share content and receive shared content small implementation

Source: Internet
Author: User

First of all, to share, after all, not sharing what to receive share to talk about?

Share there are currently implemented in two ways: background code implementation, Shareactionprovider implementation, and then first said through the code implementation

Intent intent=New  Intent (intent.action_send);intent.settype ("image/*"); Intent.putextra ( Intent.extra_stream, Uri.parse ("/storage/emulated/0/pictures/145.jpg"); StartActivity ( Intent.createchooser (Intent,"Share this image to ...");    

Here is the picture, the picture is passed in binary form, so use Intent.extra_stream to flag the additional message type.

If the text is the same principle, simply change the type and additional information to the following

Intent.settype ("Text/plain"); Intent.putextra (Intent.extra_text,"Hello World");

This simple implementation of content sharing, click to share the content system will automatically help us to find the implementation of the receiving content of the program (, QQ, Bluetooth, etc.), and show let us choose, such as

How to do it with Shareactionprovider, first add an item to the menu file

<android:id= "@+id/share"        android:orderincategory= "200"          android:title= "Share"        android:icon= "@android:d rawable/ic_menu _share "        app:showasaction=" Always "        app:actionproviderclass = "Android.support.v7.widget.ShareActionProvider" />

Here is using the V7 package under the Shareactionprovider, corresponding to the background code inside the V7 package under the class, it should be noted that the App:actionproviderclass prefix is the app, not Android, If you write Android, you will get an error and flash back. If everything is OK then the program starts to show a shared picture in the title bar, the amount, the exact icon, the Click will also show the content of the program, such as

Add finished in the background code also want to set the content to share, or how the program know what to share?

 Public BooleanOncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.menu_main, menu); MenuItem Item=Menu.finditem (R.id.share); Shareactionprovider SAP=(Shareactionprovider) Menuitemcompat.getactionprovider (item); Intent Intent=NewIntent (intent.action_send); Intent.settype ("Text/plain"); Intent.putextra (Intent.extra_text,"Hi Jiujie zhu,do u have lunch?"); if(sap!=NULL) {sap.setshareintent (intent); }        return true; }

Here is the text message.

The share is finished to receive, and then say how to receive

Reception is actually quite simple, at least more simple than I thought (I am a novice I am proud, baa hehe ~ ~ ~)

To receive pictures as an example, create a new activity, in the manifest file set action, data, category can, how with the receiver a bit like it, see the code

<intent-filter>       <action android:name= "Android.intent.action.SEND"/>       <category android: Name= "Android.intent.category.DEFAULT"/>       <data android:mimetype= "image/*"/></intent-filter>

Data represents the type of file received and is not received if it is of the text type. In this way, the system will recognize our program and show it to the user when you share the content, and look at the receivesharedcontentacitvity in the second picture, which is a custom activity for receiving content, so simple.

Light receives and doesn't work, the key is how to get the content to share and how to deal with the received content

Private voidHandleimage () {Intent Intent=getintent (); String Action=intent.getaction (); String type=Intent.gettype (); if(Action.equals (Intent.action_send) &&type.equals ("image/*") {URI Uri=Intent.getparcelableextra (Intent.extra_stream); //receive more than one picture//arraylist<uri> Uris=intent.getparcelablearraylistextra (intent.extra_stream);            if(uri!=NULL ){                Try{FileInputStream FileInputStream=NewFileInputStream (Uri.getpath ()); Bitmap Bitmap=Bitmapfactory.decodestream (FileInputStream);                Imageview.setimagebitmap (bitmap); } Catch(FileNotFoundException e) {e.printstacktrace (); }            }        }    }

The Getintent method gets the intent that contains the shared content, and then the content is available. It is stated that if you share the picture is in the SD card, please give the program to add read SD card permissions , otherwise it will show permission denied

<android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

It's all about sharing a piece of text or a picture, if you want more than one, the same! Just use the arratlist to encapsulate it when sharing, and for the receiver, change the action to

<android:name= "Android.intent.action.SEND_MULTIPLEND"/>

Represents receiving multiple content, using Getparcelablearraylistextra instead of Getparcelableextra when fetching content from intent in code.

Android share content and receive shared content small implementation

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.