Detailed implementation of cross-application data sharing in Android system _android

Source: Internet
Author: User
Tags gcd html form http post spl pkcs7 rar gtar

An important part of a andoird application is that they have the ability to communicate and integrate with each other, one application can interact with another, and then we'll look at the content sharing between Android apps.
When you build intent, you have to specify intent action triggers, define intent action such as Action_send, and you can probably guess that the action is named Action_ Send's intent is the role of sending data from one activity to another, or even crossing different processes to send data to another, you need to specify the data and his type, the system will identify the incoming activity, and show them to the user (if there are multiple choices) or start the activity immediately (as long as a choice)
1. Send text content:

The most direct and most commonly used is to use Action_send to send text content from one application to another, for example, our built-in browser can share text with the current URL of any page, and the following example is shared text content

Intent sendintent = new Intent (); 
Sendintent.setaction (intent.action_send); 
Here is the text you sent 
Sendintent.putextra (intent.extra_text, "I test sharing content with my cell phone"); 
Sendintent.settype ("Text/plain"); 
StartActivity (sendintent); 

If you install an application that filters action Action_send,mimetype as "Text/plain", the system will start, and if the system matches multiple applications like this, He's going to pop a dialog. List all applications for user selection (a selector), as shown below

Maybe you think the above code is very simple, I can easily write it, but you found that no, when our mobile phone does not have the map type of application, you execute the StartActivity method, your program will appear crash (crash), So we need to determine whether the system has this type of application, the judgment is very simple

public boolean hasapplication (Intent Intent) { 
  Packagemanager Packagemanager = Getpackagemanager (); 
  Inquire whether there is an activity of the intent 
  list<resolveinfo> activities = packagemanager.queryintentactivities (intent, 0); 
  Activities inside is not empty, otherwise there is no return 
  activities.size () > 0? true:false; 

So we have to do the above judgment in the startactivity, this way we developed the program will be more robust, less crash user experience will be better, if every time to judge your teacher will feel annoyed? So we can also use the intent Intent.createchooser method
He has the following advantages
1.1 If the user chooses the default method, the selector will still bounce out
1.2 If there is no match to such an application, the system prompts the message
1.3 You can specify the title of Selector dialog

Intent sendintent = new Intent (); 
Sendintent.setaction (intent.action_send); 
Sendintent.putextra (Intent.extra_text, "I test sharing content with my cell phone"); 
Sendintent.settype ("Text/plain"); 
StartActivity (Intent.createchooser (sendintent, "I am the title of the Pop-up Box")); 

Note: Some Mail-class applications, such as Gmail, we can use Putextra (Intent.extra_email, string[]) to add an array of strings to Intent

2. Send binary content

The sharing of binary content uses action for Action_send, setting the appropriate MIME type, and adding Putextra (Intent.extra_stream, URI), the following is a commonly used shared picture code, You can also share any type of binary content

Intent shareintent = new Intent (); 
Shareintent.setaction (intent.action_send); 
Shareintent.putextra (Intent.extra_stream, uritoimage); 
Shareintent.settype ("Image/jpeg"); 
StartActivity (Intent.createchooser (Shareintent, Getresources (). GetText (r.string.send_to))); 

The following are the main points:

    • You can use the MIME type as "*/*", and he will only match the activity that handles the general data stream
    • The receiving application requires URI to specify the access rights of the data
    • In the SD card files, we use the file File = new file (FilePath); Uri.fromfile (file), and then pass him to intent
    • In the directory of the application, the openfileoutput pattern is mode_world_readable, and then we can use Getfilestreampath () to return a file and then use Uri.fromfile (file) Pass to Intent
    • Can scan images, video and audio and other media files, add to the system Mediastore use Scanfile () to scan files, after the scan is completed, call the Onscancompleted () callback method to return a URI
    • The picture is inserted into the system using the Insertimage () method Mediastore also returns the URI of a picture

Here is an example of a shared picture:

Intent shareintent = new Intent (); 
Shareintent.setaction (intent.action_send); 
File File = new file ("Mnt/sdcard/share.png"); 
    
System.out.println (uri.fromfile (file)); 
Shareintent.putextra (Intent.extra_stream, Uri.fromfile (file)); 
Shareintent.settype ("Image/jpeg"); 
StartActivity (Intent.createchooser (shareintent, "shared picture")); 

After the selection of QQ space, the picture is shown there, the following figure

We're going to share multiple binary content. Using action for Action_send_multiple, here's how we share multiple pictures

arraylist<uri> Imageuris = new arraylist<uri> (); 
Imageuris.add (IMAGEURI1); Add your image URIs here 
imageuris.add (IMAGEURI2); 
 
Intent shareintent = new Intent (); 
Shareintent.setaction (intent.action_send_multiple); 
Shareintent.putparcelablearraylistextra (Intent.extra_stream, imageuris); 
Shareintent.settype ("image/*"); 
StartActivity (Intent.createchooser (shareintent, "Share images to ...")); 

Let me list a few common MIME type types

    • Text/plain (plain text)
    • text/html (HTML document)
    • Application/xhtml+xml (XHTML document)
    • Image/gif (GIF image)
    • Image/jpeg (JPEG image) "PHP as: Image/pjpeg"
    • Image/png (PNG image) "PHP as: Image/x-png"
    • Video/mpeg (MPEG animation)
    • Application/octet-stream (arbitrary binary data)
    • Application/pdf (PDF document)
    • Application/msword (Microsoft Word file)
    • message/rfc822 (RFC 822 form)
    • Multipart/alternative (HTML message in HTML form and plain text, same content in different forms)
    • Application/x-www-form-urlencoded (Forms submitted using the HTTP POST method)
    • Multipart/form-data (IBID., but mainly used for form submission when accompanying file upload)

Android all supported MIME type

Smimetypemap.loadentry ("Application/andrew-inset", "EZ"); 
   Smimetypemap.loadentry ("Application/dsptype", "TSP"); 
   Smimetypemap.loadentry ("Application/futuresplash", "SPL"); 
   Smimetypemap.loadentry ("Application/hta", "HTA"); 
   Smimetypemap.loadentry ("application/mac-binhex40", "hqx"); 
   Smimetypemap.loadentry ("Application/mac-compactpro", "CPT"); 
   Smimetypemap.loadentry ("Application/mathematica", "NB"); 
   Smimetypemap.loadentry ("application/msaccess", "MDB"); 
   Smimetypemap.loadentry ("Application/oda", "ODA"); 
   Smimetypemap.loadentry ("Application/ogg", "Ogg"); 
   Smimetypemap.loadentry ("Application/pdf", "pdf"); 
   Smimetypemap.loadentry ("Application/pgp-keys", "key"); 
   Smimetypemap.loadentry ("Application/pgp-signature", "PGP"); 
   Smimetypemap.loadentry ("Application/pics-rules", "PRF"); 
   Smimetypemap.loadentry ("Application/rar", "rar"); 
   Smimetypemap.loadentry ("Application/rdf+xml", "RDF"); 
 Smimetypemap.loadentry ("Application/rss+xml", "RSS");  Smimetypemap.loadentry ("Application/zip", "zip"); 
   Smimetypemap.loadentry ("Application/vnd.android.package-archive", "apk"); 
   Smimetypemap.loadentry ("Application/vnd.cinderella", "Cdy"); 
   Smimetypemap.loadentry ("Application/vnd.ms-pki.stl", "STL"); 
   Smimetypemap.loadentry ("Application/vnd.oasis.opendocument.database", "ODB"); 
   Smimetypemap.loadentry ("Application/vnd.oasis.opendocument.formula", "ODF"); 
   Smimetypemap.loadentry ("Application/vnd.oasis.opendocument.graphics", "ODG"); 
   Smimetypemap.loadentry ("Application/vnd.oasis.opendocument.graphics-template", "OTG"); 
   Smimetypemap.loadentry ("Application/vnd.oasis.opendocument.image", "ODI"); 
   Smimetypemap.loadentry ("Application/vnd.oasis.opendocument.spreadsheet", "ODS"); 
   Smimetypemap.loadentry ("Application/vnd.oasis.opendocument.spreadsheet-template", "OTs"); Smimetypemap.loadentry ("Application/vnd.oasis.opendocument.text", "ODT "); 
   Smimetypemap.loadentry ("Application/vnd.oasis.opendocument.text-master", "ODM"); 
   Smimetypemap.loadentry ("Application/vnd.oasis.opendocument.text-template", "Ott"); 
   Smimetypemap.loadentry ("Application/vnd.oasis.opendocument.text-web", "oth"); 
   Smimetypemap.loadentry ("Application/msword", "Doc"); 
   Smimetypemap.loadentry ("Application/msword", "dot"); 
   Smimetypemap.loadentry ("Application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx");  
   Smimetypemap.loadentry ("Application/vnd.openxmlformats-officedocument.wordprocessingml.template", "dotx"); 
   Smimetypemap.loadentry ("application/vnd.ms-excel", "xls"); 
   Smimetypemap.loadentry ("application/vnd.ms-excel", "XLT"); 
   Smimetypemap.loadentry ("Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx"); Smimetypemap.loadentry ("Application/vnd.openxmlformats-officedocument.spreadsheetml.template", "Xltx "); 
   Smimetypemap.loadentry ("Application/vnd.ms-powerpoint", "ppt"); 
   Smimetypemap.loadentry ("Application/vnd.ms-powerpoint", "pot"); 
   Smimetypemap.loadentry ("Application/vnd.ms-powerpoint", "pps"); 
   Smimetypemap.loadentry ("Application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx"); 
   Smimetypemap.loadentry ("Application/vnd.openxmlformats-officedocument.presentationml.template", "potx");  
   Smimetypemap.loadentry ("Application/vnd.openxmlformats-officedocument.presentationml.slideshow", "PPSX"); 
   Smimetypemap.loadentry ("application/vnd.rim.cod", "cod"); 
   Smimetypemap.loadentry ("Application/vnd.smaf", "MMF"); 
   Smimetypemap.loadentry ("Application/vnd.stardivision.calc", "SDC"); 
   Smimetypemap.loadentry ("Application/vnd.stardivision.draw", "SDA"); 
   Smimetypemap.loadentry ("Application/vnd.stardivision.impress", "SDD"); Smimetypemap.loadentry ("Application/vnd.stardivision.impresS "," SDP "); 
   Smimetypemap.loadentry ("Application/vnd.stardivision.math", "SMF"); 
   Smimetypemap.loadentry ("Application/vnd.stardivision.writer", "SDW"); 
   Smimetypemap.loadentry ("Application/vnd.stardivision.writer", "vor"); 
   Smimetypemap.loadentry ("Application/vnd.stardivision.writer-global", "SGL"); 
   Smimetypemap.loadentry ("Application/vnd.sun.xml.calc", "SXC"); 
   Smimetypemap.loadentry ("Application/vnd.sun.xml.calc.template", "STC"); 
   Smimetypemap.loadentry ("Application/vnd.sun.xml.draw", "sxd"); 
   Smimetypemap.loadentry ("Application/vnd.sun.xml.draw.template", "Std"); 
   Smimetypemap.loadentry ("Application/vnd.sun.xml.impress", "SXi"); 
   Smimetypemap.loadentry ("Application/vnd.sun.xml.impress.template", "STI"); 
   Smimetypemap.loadentry ("Application/vnd.sun.xml.math", "SXM"); 
   Smimetypemap.loadentry ("Application/vnd.sun.xml.writer", "Sxw"); Smimetypemap.loadentry ("Application/vnd.sun.xml.writer.global", "Sxg "); 
   Smimetypemap.loadentry ("Application/vnd.sun.xml.writer.template", "STW"); 
   Smimetypemap.loadentry ("Application/vnd.visio", "VSD"); 
   Smimetypemap.loadentry ("Application/x-abiword", "ABW"); 
   Smimetypemap.loadentry ("Application/x-apple-diskimage", "DMG"); 
   Smimetypemap.loadentry ("Application/x-bcpio", "Bcpio"); 
   Smimetypemap.loadentry ("application/x-bittorrent", "torrent"); 
   Smimetypemap.loadentry ("APPLICATION/X-CDF", "CDF"); 
   Smimetypemap.loadentry ("Application/x-cdlink", "VCD"); 
   Smimetypemap.loadentry ("Application/x-chess-pgn", "PGN"); 
   Smimetypemap.loadentry ("Application/x-cpio", "Cpio"); 
   Smimetypemap.loadentry ("Application/x-debian-package", "Deb"); 
   Smimetypemap.loadentry ("Application/x-debian-package", "Udeb"); 
   Smimetypemap.loadentry ("Application/x-director", "DCR"); 
   Smimetypemap.loadentry ("Application/x-director", "dir"); 
   Smimetypemap.loadentry ("Application/x-director", "DXR"); Smimetypemap.loadentry ("application/X-dms "," DMS "); 
   Smimetypemap.loadentry ("Application/x-doom", "wad"); 
   Smimetypemap.loadentry ("Application/x-dvi", "DVI"); 
   Smimetypemap.loadentry ("Application/x-flac", "FLAC"); 
   Smimetypemap.loadentry ("Application/x-font", "PFA"); 
   Smimetypemap.loadentry ("Application/x-font", "PFB"); 
   Smimetypemap.loadentry ("Application/x-font", "GSF"); 
   Smimetypemap.loadentry ("Application/x-font", "PCF"); Smimetypemap.loadentry ("Application/x-font", "PCF.") 
   Z "); 
   Smimetypemap.loadentry ("Application/x-freemind", "mm"); 
   Smimetypemap.loadentry ("Application/x-futuresplash", "SPL"); 
   Smimetypemap.loadentry ("Application/x-gnumeric", "Gnumeric"); 
   Smimetypemap.loadentry ("APPLICATION/X-GO-SGF", "SGF"); 
   Smimetypemap.loadentry ("Application/x-graphing-calculator", "GCF"); 
   Smimetypemap.loadentry ("Application/x-gtar", "Gtar"); 
   Smimetypemap.loadentry ("Application/x-gtar", "tgz"); 
   Smimetypemap.loadentry ("Application/x-gtar", "Taz"); Smimetypemap.loadentry ("ApplICATION/X-HDF "," HDF "); 
   Smimetypemap.loadentry ("Application/x-ica", "ICA"); 
   Smimetypemap.loadentry ("Application/x-internet-signup", "ins"); 
   Smimetypemap.loadentry ("Application/x-internet-signup", "ISP"); 
   Smimetypemap.loadentry ("Application/x-iphone", "III"); 
   Smimetypemap.loadentry ("Application/x-iso9660-image", "ISO"); 
   Smimetypemap.loadentry ("Application/x-jmol", "JMZ"); 
   Smimetypemap.loadentry ("Application/x-kchart", "Chrt"); 
   Smimetypemap.loadentry ("Application/x-killustrator", "Kil"); 
   Smimetypemap.loadentry ("Application/x-koan", "SKP"); 
   Smimetypemap.loadentry ("Application/x-koan", "SKD"); 
   Smimetypemap.loadentry ("Application/x-koan", "Skt"); 
   Smimetypemap.loadentry ("Application/x-koan", "SKM"); 
   Smimetypemap.loadentry ("Application/x-kpresenter", "KPR"); 
   Smimetypemap.loadentry ("Application/x-kpresenter", "kpt"); 
   Smimetypemap.loadentry ("Application/x-kspread", "KSP"); 
   Smimetypemap.loadentry ("Application/x-kword", "KWD"); SMimetypemap.loadentry ("Application/x-kword", "KWT"); 
   Smimetypemap.loadentry ("Application/x-latex", "latex"); 
   Smimetypemap.loadentry ("Application/x-lha", "Lha"); 
   Smimetypemap.loadentry ("Application/x-lzh", "LZH"); 
   Smimetypemap.loadentry ("Application/x-lzx", "LZX"); 
   Smimetypemap.loadentry ("Application/x-maker", "frm"); 
   Smimetypemap.loadentry ("Application/x-maker", "maker"); 
   Smimetypemap.loadentry ("Application/x-maker", "frame"); 
   Smimetypemap.loadentry ("Application/x-maker", "FB"); 
   Smimetypemap.loadentry ("Application/x-maker", "book"); 
   Smimetypemap.loadentry ("Application/x-maker", "Fbdoc"); 
   Smimetypemap.loadentry ("Application/x-mif", "MIF"); 
   Smimetypemap.loadentry ("APPLICATION/X-MS-WMD", "WMD"); 
   Smimetypemap.loadentry ("Application/x-ms-wmz", "wmz"); 
   Smimetypemap.loadentry ("Application/x-msi", "MSI"); 
   Smimetypemap.loadentry ("Application/x-ns-proxy-autoconfig", "PAC"); 
   Smimetypemap.loadentry ("APPLICATION/X-NWC", "NWC"); SmiMetypemap.loadentry ("Application/x-object", "O"); 
   Smimetypemap.loadentry ("Application/x-oz-application", "Oza"); 
   Smimetypemap.loadentry ("Application/x-pkcs12", "P12"); 
   Smimetypemap.loadentry ("Application/x-pkcs7-certreqresp", "p7r"); 
   Smimetypemap.loadentry ("Application/x-pkcs7-crl", "CRL"); 
   Smimetypemap.loadentry ("Application/x-quicktimeplayer", "qtl"); 
   Smimetypemap.loadentry ("Application/x-shar", "Shar"); 
   Smimetypemap.loadentry ("Application/x-shockwave-flash", "SwF"); 
   Smimetypemap.loadentry ("Application/x-stuffit", "sit"); 
   Smimetypemap.loadentry ("Application/x-sv4cpio", "Sv4cpio"); 
   Smimetypemap.loadentry ("APPLICATION/X-SV4CRC", "SV4CRC"); 
   Smimetypemap.loadentry ("Application/x-tar", "tar"); 
   Smimetypemap.loadentry ("Application/x-texinfo", "Texinfo"); 
   Smimetypemap.loadentry ("Application/x-texinfo", "Texi"); 
   Smimetypemap.loadentry ("Application/x-troff", "T"); 
   Smimetypemap.loadentry ("Application/x-troff", "Roff"); Smimetypemap. Loadentry ("Application/x-troff-man", "man"); 
   Smimetypemap.loadentry ("Application/x-ustar", "Ustar"); 
   Smimetypemap.loadentry ("Application/x-wais-source", "src"); 
   Smimetypemap.loadentry ("Application/x-wingz", "wz"); 
   Smimetypemap.loadentry ("Application/x-webarchive", "webarchive"); 
   Smimetypemap.loadentry ("Application/x-x509-ca-cert", "CRT"); 
   Smimetypemap.loadentry ("Application/x-x509-user-cert", "CRT"); 
   Smimetypemap.loadentry ("APPLICATION/X-XCF", "XCF"); 
   Smimetypemap.loadentry ("Application/x-xfig", "fig"); 
   Smimetypemap.loadentry ("Application/xhtml+xml", "XHTML"); 
   Smimetypemap.loadentry ("AUDIO/3GPP", "3GPP"); 
   Smimetypemap.loadentry ("Audio/basic", "snd"); 
   Smimetypemap.loadentry ("Audio/midi", "mid"); 
   Smimetypemap.loadentry ("Audio/midi", "midi"); 
   Smimetypemap.loadentry ("Audio/midi", "Kar"); 
   Smimetypemap.loadentry ("Audio/mpeg", "MPGA"); 
   Smimetypemap.loadentry ("Audio/mpeg", "Mpega"); Smimetypemap.loadentry ("Audio/mpeg", "MP2"); 
   Smimetypemap.loadentry ("Audio/mpeg", "MP3"); 
   Smimetypemap.loadentry ("Audio/mpeg", "m4a"); 
   Smimetypemap.loadentry ("Audio/mpegurl", "m3u"); 
   Smimetypemap.loadentry ("Audio/prs.sid", "Sid"); 
   Smimetypemap.loadentry ("Audio/x-aiff", "AIF"); 
   Smimetypemap.loadentry ("Audio/x-aiff", "AIFF"); 
   Smimetypemap.loadentry ("Audio/x-aiff", "aifc"); 
   Smimetypemap.loadentry ("audio/x-gsm", "GSM"); 
   Smimetypemap.loadentry ("Audio/x-mpegurl", "m3u"); 
   Smimetypemap.loadentry ("Audio/x-ms-wma", "WMA"); 
   Smimetypemap.loadentry ("Audio/x-ms-wax", "wax"); 
   Smimetypemap.loadentry ("Audio/x-pn-realaudio", "Ra"); 
   Smimetypemap.loadentry ("Audio/x-pn-realaudio", "rm"); 
   Smimetypemap.loadentry ("Audio/x-pn-realaudio", "Ram"); 
   Smimetypemap.loadentry ("Audio/x-realaudio", "Ra"); 
   Smimetypemap.loadentry ("Audio/x-scpls", "pls"); 
   Smimetypemap.loadentry ("Audio/x-sd2", "SD2"); 
   Smimetypemap.loadentry ("Audio/x-wav", "wav"); 
Smimetypemap.loadentry ("Image/bmp", "BMP");   Smimetypemap.loadentry ("Image/gif", "gif"); 
   Smimetypemap.loadentry ("Image/ico", "cur"); 
   Smimetypemap.loadentry ("Image/ico", "ico"); 
   Smimetypemap.loadentry ("Image/ief", "ief"); 
   Smimetypemap.loadentry ("Image/jpeg", "jpeg"); 
   Smimetypemap.loadentry ("Image/jpeg", "jpg"); 
   Smimetypemap.loadentry ("Image/jpeg", "JPE"); 
   Smimetypemap.loadentry ("image/pcx", "PCX"); 
   Smimetypemap.loadentry ("Image/png", "PNG"); 
   Smimetypemap.loadentry ("Image/svg+xml", "svg"); 
   Smimetypemap.loadentry ("Image/svg+xml", "Svgz"); 
   Smimetypemap.loadentry ("Image/tiff", "tiff"); 
   Smimetypemap.loadentry ("Image/tiff", "TIF"); 
   Smimetypemap.loadentry ("Image/vnd.djvu", "DjVu"); 
   Smimetypemap.loadentry ("Image/vnd.djvu", "DJV"); 
   Smimetypemap.loadentry ("Image/vnd.wap.wbmp", "wbmp"); 
   Smimetypemap.loadentry ("Image/x-cmu-raster", "Ras"); 
   Smimetypemap.loadentry ("Image/x-coreldraw", "CDR"); 
   Smimetypemap.loadentry ("Image/x-coreldrawpattern", "Pat"); Smimetypemap.loaDEntry ("Image/x-coreldrawtemplate", "CDT"); 
   Smimetypemap.loadentry ("Image/x-corelphotopaint", "CPT"); 
   Smimetypemap.loadentry ("Image/x-icon", "ico"); 
   Smimetypemap.loadentry ("Image/x-jg", "art"); 
   Smimetypemap.loadentry ("Image/x-jng", "Jng"); 
   Smimetypemap.loadentry ("Image/x-ms-bmp", "BMP"); 
   Smimetypemap.loadentry ("Image/x-photoshop", "PSD"); 
   Smimetypemap.loadentry ("Image/x-portable-anymap", "PNM"); 
   Smimetypemap.loadentry ("Image/x-portable-bitmap", "PBM"); 
   Smimetypemap.loadentry ("Image/x-portable-graymap", "PGM"); 
   Smimetypemap.loadentry ("Image/x-portable-pixmap", "ppm"); 
   Smimetypemap.loadentry ("Image/x-rgb", "RGB"); 
   Smimetypemap.loadentry ("Image/x-xbitmap", "XBM"); 
   Smimetypemap.loadentry ("Image/x-xpixmap", "xpm"); 
   Smimetypemap.loadentry ("Image/x-xwindowdump", "xwd"); 
   Smimetypemap.loadentry ("Model/iges", "IGs"); 
   Smimetypemap.loadentry ("Model/iges", "iges"); 
   Smimetypemap.loadentry ("Model/mesh", "MSH"); Smimetypemap.loadeNtry ("Model/mesh", "mesh"); 
   Smimetypemap.loadentry ("Model/mesh", "silo"); 
   Smimetypemap.loadentry ("Text/calendar", "ICS"); 
   Smimetypemap.loadentry ("Text/calendar", "Icz"); 
   Smimetypemap.loadentry ("Text/comma-separated-values", "CSV"); 
   Smimetypemap.loadentry ("Text/css", "CSS"); 
   Smimetypemap.loadentry ("text/html", "htm"); 
   Smimetypemap.loadentry ("text/html", "html"); 
   Smimetypemap.loadentry ("text/h323", "323"); 
   Smimetypemap.loadentry ("Text/iuls", "uls"); 
   Smimetypemap.loadentry ("Text/mathml", "MML"); 
   Add it's it is the default for Extensionfrommimetype smimetypemap.loadentry ("Text/plain", "txt"); 
   Smimetypemap.loadentry ("Text/plain", "ASC"); 
   Smimetypemap.loadentry ("Text/plain", "text"); 
   Smimetypemap.loadentry ("Text/plain", "diff");  Smimetypemap.loadentry ("Text/plain", "po"); 
   Reserve "pot" for Vnd.ms-powerpoint smimetypemap.loadentry ("Text/richtext", "Rtx"); 
   Smimetypemap.loadentry ("Text/rtf", "RTF"); SMimetypemap.loadentry ("Text/texmacs", "TS"); 
   Smimetypemap.loadentry ("Text/text", "Phps"); 
   Smimetypemap.loadentry ("Text/tab-separated-values", "TSV"); 
   Smimetypemap.loadentry ("Text/xml", "xml"); 
   Smimetypemap.loadentry ("Text/x-bibtex", "bib"); 
   Smimetypemap.loadentry ("Text/x-boo", "Boo"); 
   Smimetypemap.loadentry ("Text/x-c++hdr", "h++"); 
   Smimetypemap.loadentry ("Text/x-c++hdr", "hpp"); 
   Smimetypemap.loadentry ("Text/x-c++hdr", "hxx"); 
   Smimetypemap.loadentry ("Text/x-c++hdr", "hh"); 
   Smimetypemap.loadentry ("Text/x-c++src", "C + +"); 
   Smimetypemap.loadentry ("Text/x-c++src", "CPP"); 
   Smimetypemap.loadentry ("Text/x-c++src", "cxx"); 
   Smimetypemap.loadentry ("Text/x-chdr", "H"); 
   Smimetypemap.loadentry ("Text/x-component", "HTC"); 
   Smimetypemap.loadentry ("Text/x-csh", "csh"); 
   Smimetypemap.loadentry ("Text/x-csrc", "C"); 
   Smimetypemap.loadentry ("Text/x-dsrc", "D"); 
   Smimetypemap.loadentry ("Text/x-haskell", "HS"); Smimetypemap.loadentry ("TexT/x-java "," Java "); 
   Smimetypemap.loadentry ("Text/x-literate-haskell", "LHS"); 
   Smimetypemap.loadentry ("Text/x-moc", "MOC"); 
   Smimetypemap.loadentry ("Text/x-pascal", "P"); 
   Smimetypemap.loadentry ("text/x-pascal", "pas"); 
   Smimetypemap.loadentry ("TEXT/X-PCS-GCD", "GCD"); 
   Smimetypemap.loadentry ("Text/x-setext", "ETX"); 
   Smimetypemap.loadentry ("Text/x-tcl", "Tcl"); 
   Smimetypemap.loadentry ("Text/x-tex", "Tex"); 
   Smimetypemap.loadentry ("Text/x-tex", "LTX"); 
   Smimetypemap.loadentry ("Text/x-tex", "sty"); 
   Smimetypemap.loadentry ("Text/x-tex", "CLS"); 
   Smimetypemap.loadentry ("Text/x-vcalendar", "VCs"); 
   Smimetypemap.loadentry ("Text/x-vcard", "VCF"); 
   Smimetypemap.loadentry ("VIDEO/3GPP", "3GPP"); 
   Smimetypemap.loadentry ("VIDEO/3GPP", "3gp"); 
   Smimetypemap.loadentry ("VIDEO/3GPP", "3g2"); 
   Smimetypemap.loadentry ("video/dl", "DL"); 
   Smimetypemap.loadentry ("Video/dv", "dif"); 
   Smimetypemap.loadentry ("Video/dv", "DV"); Smimetypemap.loadentRy ("Video/fli", "Fli"); 
   Smimetypemap.loadentry ("video/m4v", "M4V"); 
   Smimetypemap.loadentry ("Video/mpeg", "MPEG"); 
   Smimetypemap.loadentry ("Video/mpeg", "mpg"); 
   Smimetypemap.loadentry ("Video/mpeg", "MPE"); 
   Smimetypemap.loadentry ("Video/mp4", "mp4"); 
   Smimetypemap.loadentry ("Video/mpeg", "VOB"); 
   Smimetypemap.loadentry ("Video/quicktime", "QT"); 
   Smimetypemap.loadentry ("Video/quicktime", "mov"); 
   Smimetypemap.loadentry ("Video/vnd.mpegurl", "Mxu"); 
   Smimetypemap.loadentry ("video/x-la-asf", "LSF"); 
   Smimetypemap.loadentry ("video/x-la-asf", "lsx"); 
   Smimetypemap.loadentry ("Video/x-mng", "MNG"); 
   Smimetypemap.loadentry ("video/x-ms-asf", "ASF"); 
   Smimetypemap.loadentry ("video/x-ms-asf", "ASX"); 
   Smimetypemap.loadentry ("Video/x-ms-wm", "WM"); 
   Smimetypemap.loadentry ("video/x-ms-wmv", "WMV"); 
   Smimetypemap.loadentry ("Video/x-ms-wmx", "wmx"); 
   Smimetypemap.loadentry ("Video/x-ms-wvx", "wvx"); Smimetypemap.loadentry ("Video/x-msvideo", "aVI "); 
   Smimetypemap.loadentry ("Video/x-sgi-movie", "movie"); 
   Smimetypemap.loadentry ("X-conference/x-cooltalk", "ice"); 
 Smimetypemap.loadentry ("X-epoc/x-sisx-app", "SISX");

3. Receive a picture            
To share what is received from another application, For example, if you develop a social networking application where one of your activities can accept things that people share from other applications, such as sharing text or sharing pictures from a gallery, and so on, we'll use an example to explain what we accept from other applications.
We're creating a new Android project, Name is Sharedcontext, modify manifest file
We need to define in manifest file What intent this activity can receive, we need to create intent filter, use <intent-filter > elements to filter the intent we can receive, below we give a simple example, I believe you know extrapolate, the following example of our application can handle text, text files, single pictures and multiple pictures, we define manifest file as follows

&lt;?xml version= "1.0" encoding= "Utf-8"?&gt; &lt;manifest xmlns:android= "http://schemas.android.com/apk/res/"  
  Android "package=" Com.example.sharedcontext "android:versioncode=" 1 "android:versionname=" 1.0 "&gt; &lt;uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/&gt;" &lt;application android:allowbackup= "true" droid:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" &gt; &lt;acti Vity android:name= "com.example.sharedcontext.MainActivity" android:label= "@string/app_name" &gt; &lt;!-- Application startup Intent, our example has only one activity--&gt; &lt;intent-filter&gt; &lt;action android:name= "Android.intent.action.MAIN" /&gt; &lt;category android:name= "Android.intent.category.LAUNCHER"/&gt; &lt;/intent-filter&gt; &lt;!--processing Word Intent we need to define the action, category, and text corresponding to the MIME--&gt; &lt;intent-filter&gt; &lt;action android:name= "Android.intent". Action. SEND "/&gt; &lt;category Android:name= "Android.intent.category.DEFAULT"/&gt; &lt;data android:mimetype= "text/*"/&gt; &lt;/intent-filter &gt; &lt;!--intent--&gt; &lt;intent-filter&gt; &lt;action android:name= for single picture "Android.intent.action.SEND "/&gt; &lt;category android:name=" Android.intent.category.DEFAULT/&gt; &lt;data "android:mimetype=" image/* 
   Gt &lt;/intent-filter&gt; &lt;!--handle multiple pictures of intent--&gt; &lt;intent-filter&gt; &lt;action android:name= "Android. Intent.action.SEND_MULTIPLE "/&gt; &lt;category android:name=" Android.intent.category.DEFAULT "/&gt; &lt;data 
 Droid:mimetype= "image/*"/&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application &lt;/manifest&gt;

When an application constructs a intent like that, and it passes to StartActivity (), our application is listed in the intent selector, When the user chooses the application to enter the corresponding activity (the example above is mainactivity), we only need to handle the content in mainactivity and display it with the corresponding UI, mainactivity the following

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.AdapterView.OnItemClickListener; 
Import Android.widget.BaseAdapter; 
Import Android.widget.GridView; 
Import Android.widget.ImageView; 
 
Import Android.widget.TextView; public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {Super 
  . OnCreate (Savedinstancestate); 
  Intent Intent = Getintent (); 
  Gets the intent action String action = Intent.getaction (); 
   
   
  Gets intent MIME type String type = Intent.gettype (); if (Intent.ACTION_SEND.equals (ACTION) &amp;&amp; type!= null) {//We handle all text types here 
   if (Type.startswith ("text/")) {//process acquired text, here we use TextView to display handlesendtext (intent); //The MIME type of the picture has image/png, IMAGE/JEPG, image/gif etc., else if (Type.startswith ("image/")) {//processing get to the picture, we use image 
   View Display handlesendimage (intent);  } else if (Intent.ACTION_SEND_MULTIPLE.equals (ACTION) &amp;&amp; type!= null) {if (Type.startswith ("image/")) 
    {//Processing multiple pictures, we use a GridView to display handlesendmultipleimages (intent); /** * display text with TextView * can open general text file * @param intent/private void handlesendtext (intent int 
   
   ENT) {TextView TextView = new TextView (this); 
   For general text processing, we directly display string sharedtext = Intent.getstringextra (Intent.extra_text); 
   if (Sharedtext!= null) {Textview.settext (sharedtext); 
   //text file processing, get the input stream from the URI, and then replace the input flow with the string Uri Texturi = (Uri) Intent.getparcelableextra (Intent.extra_stream); if (Texturi!= null) {try {InputStream InputStream = this.getcontentResolver (). Openinputstream (Texturi); 
   Textview.settext (Inputstream2byte (InputStream)); 
   catch (Exception e) {e.printstacktrace (); 
 }//Set to Activity Setcontentview (TextView); /** * Convert input flow to String * @param inputstream * @return * @throws IOException */private string INPUTST 
   
  Ream2byte (InputStream inputstream) throws ioexception{bos = new Bytearrayoutputstream (); 
  byte [] buffer = new byte[1024]; 
   
  int len =-1; 
  while (len = inputstream.read (buffer))!=-1) {bos.write (buffer, 0, Len); 
   
  } bos.close (); 
 Specifies that the encoding format is UIT-8 return new String (Bos.tobytearray (), "UTF-8"); /** * Display single picture with ImageView * @param intent/private void handlesendimage (Intent intent) {Uri Imageu 
  RI = (Uri) Intent.getparcelableextra (Intent.extra_stream); 
   if (Imageuri!= null) {ImageView ImageView = new ImageView (this); 
   Imageview.setimageuri (Imageuri); Setcontentview(ImageView); 
  }/** * Display multiple pictures in GridView * @param intent/private void handlesendmultipleimages (intent intent) { 
  Final arraylist&lt;uri&gt; Imageuris = Intent.getparcelablearraylistextra (Intent.extra_stream); 
   if (Imageuris!= null) {GridView GridView = new GridView (this); 
   Set the width of the item gridview.setcolumnwidth (130); 
   The set column is automatically adapted to Gridview.setnumcolumns (Gridview.auto_fit); 
   Gridview.setadapter (This, Imageuris) (new Gridadapter); 
    
   Setcontentview (GridView); Gridview.setonitemclicklistener (New Onitemclicklistener () {@Override public void Onitemclick (ADAPTERVIEW&LT;? &gt; Parent, view view, final int position, long ID) {//Click on the item of the GridView to share pictures for other applications//Here you can refer to Htt 
     p://blog.csdn.net/xiaanming/article/details/9395991 Intent Intent = new Intent (); 
     Intent.setaction (Intent.action_send); 
     Intent.putextra (Intent.extra_stream, imageuris.get (position)); 
   Intent.settype ("image/*");  StartActivity (Intent.createchooser (Intent, "shared picture")); 
    
  } 
   }); }/** * Rewrite Baseadapter * @author xiaanming * */public class Gridadapter extends baseadapter{pri 
  Vate context Mcontext; 
   
  Private arraylist&lt;uri&gt; list; 
   Public Gridadapter (Context Mcontext, arraylist&lt;uri&gt; list) {this.list = list; 
  This.mcontext = Mcontext; 
  @Override public int GetCount () {return list.size (); 
  @Override public Object getitem (int position) {return list.get (position); 
  @Override public long getitemid (int position) {return position; 
   @Override public View getview (int position, View Convertview, ViewGroup parent) {ImageView 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, we then select the System Gallery, select more than one picture (figure I) to share, our own application sharing multiple interfaces (figure II) Click on our application item, choose to share a single picture (figure III) We continue to select our own application to display (Figure IV), Create a new memo save, long by memo share (Figure V), share text file display interface (Figure VI)

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.