当前位置: 移动技术网 > 移动技术>移动开发>Android > 详解Android系统中跨应用数据分享功能的实现

详解Android系统中跨应用数据分享功能的实现

2019年07月24日  | 移动技术网移动技术  | 我要评论

一个andoird应用程序的重要的地方是他们有相互沟通和整合的能力,一个应用程序可以和另一个应用程序交互,接下来我们来看看android应用之间的内容分享
当你构建intent的时候,必须要指定intent的action触发,定义intent的action例如action_send,你大概可以猜到指定action为action_send的intent的作用是从一个activity发送数据到另一个activity,甚至跨越不同的进程,将数据发送给另一个activity,你需要指定数据和他的类型,系统将识别接收的activity,并显示他们给用户(假如有多个选择的时候)或者立即启动activity(只要一个选择的时候)
1.发送文本内容:

最直接最常用的是用action_send来从一个应用发送文本内容到另一个应用,例如,我们的内置浏览器可以与任何页面当前的url来共享文字,下面的例子就是共享文本内容

intent sendintent = new intent(); 
sendintent.setaction(intent.action_send); 
//这里是你发送的文本 
sendintent.putextra(intent.extra_text, "我用手机测试分享内容"); 
sendintent.settype("text/plain"); 
startactivity(sendintent); 

假如你安装了过滤action为action_send,mimetype为“text/plain”的应用程序,这个系统就会启动,假如系统匹配到多个这样子的应用程序,他就会弹出一个dialog列出所有的应用供用户选择(一个选择器),如下图

201641152740097.png (290×550)

可能你觉得上面的代码很简单,我很容易就能写出来,可是你发现了没有,当我们的手机里面没有地图类型的应用程序,你执行startactivity方法,你的程序就会出现crash(崩溃),所以我们需要先判断下系统是否有这类型的应用程序,判断也很简单

public boolean hasapplication(intent intent){ 
  packagemanager packagemanager = getpackagemanager(); 
  //查询是否有该intent的activity 
  list<resolveinfo> activities = packagemanager.queryintentactivities(intent, 0); 
  //activities里面不为空就有,否则就没有 
  return activities.size() > 0 ? true : false; 

所以我们在startactivity的时候要做上面的判断,这样子我们开发出来的程序才会更加的健壮,少一些crash用户体验也会更好,假如每次都要判断你师傅会觉得烦?所以我们也可以使用intent的intent.createchooser方法
他有如下优点
1.1 即使用户选择了默认的方式,选择器仍然会弹出来
1.2 如果没有匹配到这样的应用程序,系统就会提示消息
1.3 你可以指定选择器dialog的标题

intent sendintent = new intent(); 
sendintent.setaction(intent.action_send); 
sendintent.putextra(intent.extra_text, "我用手机测试分享内容"); 
sendintent.settype("text/plain"); 
startactivity(intent.createchooser(sendintent, "我是弹出框的标题")); 

注:某些邮件类应用程序,如gmail,我们可以使用putextra(intent.extra_email, string[]) 添加一个字符串数组到intent中

2.发送二进制内容

二进制内容的共享使用action为action_send,在设置适当的mime类型,并且添加putextra(intent.extra_stream, uri),下面是一个常用的共享图片的代码,也可以共享任何类型的二进制内容

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))); 

主要下面几点:

  • 你可以使用mime type为“ */*” ,他只会匹配处理一般数据流的activity
  • 接收应用程序需要uri指定数据的访问权限
  • 在sd卡的文件,我们使用file file = new file(filepath); uri.fromfile(file),然后把他传递给intent
  • 在应用程序的目录中,openfileoutput的模式为mode_world_readable,然后我们可以使用getfilestreampath()返回一个file,然后利用uri.fromfile(file)传递给intent
  • 可以扫描图像,视频和音频等媒体文件,添加到系统mediastore使用scanfile()来扫描文件,扫描完成后会调用 onscancompleted()回调方法来返回一个uri
  • 图片使用insertimage()方法插入到系统mediastore也会返回一个图片的uri

下面是共享图片的例子:

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, "共享图片")); 

选择qq空间后,图片就显示在那里了,如下图

201641152853269.png (290×550)

我们要共享多个二进制内容利用action为action_send_multiple,下面是我们共享多张图片

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..")); 

下面我列出几个常用的mime type类型

  • text/plain(纯文本)
  • text/html(html文档)
  • application/xhtml+xml(xhtml文档)
  • image/gif(gif图像)
  • image/jpeg(jpeg图像)【php中为:image/pjpeg】
  • image/png(png图像)【php中为:image/x-png】
  • video/mpeg(mpeg动画)
  • application/octet-stream(任意的二进制数据)
  • application/pdf(pdf文档)
  • application/msword(microsoft word文件)
  • message/rfc822(rfc 822形式)
  • multipart/alternative(html邮件的html形式和纯文本形式,相同内容使用不同形式表示)
  • application/x-www-form-urlencoded(使用http的post方法提交的表单)
  • multipart/form-data(同上,但主要用于表单提交时伴随文件上传的场合)

android所有支持的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 first so it will be 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.接收图片          
下面要分享的是从其他的应用程序接收内容,比如你开发了一个社交网络的应用,你其中的某个activity可以接受人们从其他的应用分享的东西,例如分享文字或者从图库分享图片等等,接下来我们利用一个例子来讲解从其他的应用接受内容
我们新建一个android工程,名字为sharedcontext,修改manifest文件
我们需要在manifest文件中定义这个activity能接收什么样的intent,我们需要创建intent过滤器,使用 <intent-filter> 元素来过滤我们能接收的intent,下面我们举个简单的例子,相信大家知道举一反三,下面的例子我们的应用程序可以处理文本,文本文件,单一的图片和多张图片,我们定义manifest文件如下

<?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.example.sharedcontext.mainactivity" 
   android:label="@string/app_name" > 
    
   <!-- 应用程序的启动 intent,我们例子只有一个activity--> 
   <intent-filter> 
    <action android:name="android.intent.action.main" /> 
    <category android:name="android.intent.category.launcher" /> 
   </intent-filter> 
 
   <!-- 处理文字的intent 我们需要定义action, category, 和文字对应的mime --> 
   <intent-filter> 
    <action android:name="android.intent.action.send" /> 
    <category android:name="android.intent.category.default" /> 
    <data android:mimetype="text/*" /> 
   </intent-filter> 
 
   <!-- 处理单张图片的intent --> 
   <intent-filter> 
    <action android:name="android.intent.action.send" /> 
    <category android:name="android.intent.category.default" /> 
    <data android:mimetype="image/*" /> 
   </intent-filter> 
 
   <!-- 处理多张图片的intent --> 
   <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> 

当一个应用程序构建上面那样子的intent,并吧它传递给startactivity(),我们的应用程序就会被列在intent选择器中,当用户选择该应用程序就进入相对应的activity(上面的例子是mainactivity),我们只需要在mainactivity处理这样内容并用相对应的ui显示, mainactivity如下

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(); 
  //获得intent的action 
  string action = intent.getaction(); 
  //获得intent的mime type 
  string type = intent.gettype(); 
   
   
  if(intent.action_send.equals(action) && type != null){ 
   //我们这里处理所有的文本类型 
   if(type.startswith("text/")){ 
    //处理获取到的文本,这里我们用textview显示 
    handlesendtext(intent); 
   } 
   //图片的mime type有 image/png , image/jepg, image/gif 等, 
   else if(type.startswith("image/")){ 
    //处理获取到图片,我们用imageview显示 
    handlesendimage(intent); 
   } 
  } 
  else if(intent.action_send_multiple.equals(action) && type != null){ 
    if (type.startswith("image/")) { 
     //处理多张图片,我们用一个gridview来显示 
     handlesendmultipleimages(intent); 
    } 
  } 
 } 
 
 
 /** 
  * 用textview显示文本 
  * 可以打开一般的文本文件 
  * @param intent 
  */ 
 private void handlesendtext(intent intent){ 
   textview textview = new textview(this); 
   
   //一般的文本处理,我们直接显示字符串 
   string sharedtext = intent.getstringextra(intent.extra_text); 
   if(sharedtext != null){ 
    textview.settext(sharedtext); 
   } 
   
   
   //文本文件处理,从uri中获取输入流,然后将输入流转换成字符串 
   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(); 
   } 
   } 
   
   //设置给activity 
   setcontentview(textview); 
 } 
  
  
 /** 
  * 将输入流转换成字符串 
  * @param inputstream 
  * @return 
  * @throws ioexception 
  */ 
 private string inputstream2byte(inputstream 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(); 
   
  //指定编码格式为uit-8 
  return new string(bos.tobytearray(), "utf-8"); 
 } 
  
  
 /** 
  * 用imageview显示单张图片 
  * @param intent 
  */ 
 private void handlesendimage(intent intent) { 
  uri imageuri = (uri) intent.getparcelableextra(intent.extra_stream); 
  if (imageuri != null) { 
   imageview imageview = new imageview(this); 
   imageview.setimageuri(imageuri); 
   setcontentview(imageview); 
  } 
 } 
  
  
 /** 
  * 用gridview显示多张图片 
  * @param intent 
  */ 
 private void handlesendmultipleimages(intent intent) { 
  final arraylist<uri> imageuris = intent.getparcelablearraylistextra(intent.extra_stream); 
  if (imageuris != null) { 
   gridview gridview = new gridview(this); 
   //设置item的宽度 
   gridview.setcolumnwidth(130); 
   //设置列为自动适应 
   gridview.setnumcolumns(gridview.auto_fit); 
   gridview.setadapter(new gridadapter(this, imageuris)); 
   setcontentview(gridview); 
    
   gridview.setonitemclicklistener(new onitemclicklistener() { 
 
    @override 
    public void onitemclick(adapterview<?> parent, view view, 
      final int position, long id) { 
      
     //点击gridview的item 可以分享图片给其他应用 
     //这里可以参考http://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, "共享图片")); 
    } 
   }); 
    
  } 
 } 
  
 /** 
  * 重写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; 
  } 
 
  @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; 
  } 
 } 
} 

运行程序之后,然后我们选择系统图库,选择多张图片(如图一)进行分享,我们自己的应用程序分享多张界面(如图二)点击我们应用程序的item,选择分享单张图片(如图三)我们继续选择我们自己的应用程序来显示(如图四),新建一个备忘录保存,长按备忘录进行分享(如图五),分享文本文件的显示界面(如图六)

201641153824119.jpg (287×547)201641153720854.png (290×550)201641153847099.jpg (289×550)201641153905393.png (290×550)201641153927910.png (290×550)201641153949258.png (290×550)

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网