import java.io.file;import java.io.fileoutputstream;import java.io.ioexception;import java.io.inputstream;import android.net.uri;import android.os.bundle;import android.os.environment;import android.app.activity;import android.content.intent;import android.view.actionprovider;import android.view.menu;import android.view.menuitem;import android.widget.shareactionprovider;public class mainactivity extends activity { @Override protected void oncreate (bundle savedinstancestate) { super.oncreate (savedInstanceState); setcontentview (R.layout.activity_main); } @Override public boolean oncreateoptionsmenu ( Menu menu)  {       &NBSp;// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). Inflate (r.menu.main, menu); // get menu items menuitem item = menu.finditem (R.id.action_fuck_share); // get objects for provider /** * ShareActionProvider Share the action provider */ ShareActionProvider provider = (Shareactionprovider) item .getactionprovider (); // set up a sharedIntent intent shareintent = new intent (); sharetext (provider, shareintent); return true; } private void Sharetext (shareactionprovider provider, intent shareintent) { // Implicit Jump shareintent.setaction ( Intent.action_send); /** * Set the intent */ shareintent.putextra (intent.extra_text, "shared content"); shareintent.settype ("text/*"); Provider.setshareintent (shareintent); } private void shareimage (ShareActionProvider provider, intent shareintent) { // Implicit jump shareintent.setaction (intent.action_send); // purpose: will assets files, put on SD, and then analyze the image of SD card, note: Must be written to the SD to share out try { // Get assets The following IMG file to get an input stream inputstream is = getassets (). Open ("Beijin.jpeg"); // judgment mounts, and permissions if (! Environment.getexternalstoragestate (). Equals ( environment.media_mounted)) { return; } //is placed under the root path of the external store file file = new file (Environment.getexternalstoragedirectory () .getabsolutepath () + file.separator+ "Img.jpg"); Create a file if //does not exist if (! File.exists ()) { file.createnewfile (); } fileoutputstream fos = new fileoutputstream (file); int len = 0; byte[] buff = new byte[1024]; while (Len=is.read (Buff))!=-1) { fos.write (Buff, 0, len); } fos.close (); is.close (); } catch (ioexception e) { // TODO Auto-generated catch block e.printstacktrace (); } //identifier, file corresponding to the uri uri uri = uri.fromfile (New file (environment .getexternalstoragedirectory () .getabsolutepath () + file.separator+ "Img.jpg")); /** * Set the intent */ shareintent.putextra (Intent.extra_stream, uri.tostring ()); shareintent.settype ("image/*"); provider.setshareintent (shareintent); }}
Android MenuItem Share