Android MenuItem Share

Source: Internet
Author: User

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

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.