android如何实现文件打开方式可供选择功能

来源:互联网 发布:汽车改装设计软件 编辑:程序博客网 时间:2024/04/30 06:46

android如何实现文件打开方式可供选择功能


android如何实现文件打开方式可供选择功能

比如通过文档查看器打开一个文本文件时,会弹出一个可用来打开的软件列表;
如何让自己的软件也出现在该列表中呢? 通过设置AndroidManifest.xml文件即可:

<activity android:name=".EasyNote" android:label="@string/app_name" android:launchMode="singleTask" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<data android:mimeType="text/plain"></data>
</intent-filter>
</activity>
第一个<intent-filter>标签是每个程序都有的,关键是要添加第二个!这样你的应用程序就会出现在默认打开列表了。。。

注意需要将mimeType修改成你需要的类型,文本文件当然就是:text/plain

还有其它常用的如:

    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(同上,但主要用于表单提交时伴随文件上传的场合)





private void openFile(File f)  
 {  
   Intent intent = new Intent();  
   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
   intent.setAction(android.content.Intent.ACTION_VIEW);  
   String type = getMIMEType(f);  
   intent.setDataAndType(Uri.fromFile(f), type);  
   startActivity(intent);  
 }  
 
 private String getMIMEType(File f){  
   String end = f  
       .getName()  
       .substring(f.getName().lastIndexOf(".") + 1,  
           f.getName().length()).toLowerCase();  
   String type = "";  
   if (end.equals("mp3") || end.equals("aac") || end.equals("aac")  
       || end.equals("amr") || end.equals("mpeg")  
       || end.equals("mp4"))  
   {  
     type = "audio";  
   } else if (end.equals("jpg") || end.equals("gif")  
       || end.equals("png") || end.equals("jpeg"))  
   {  
     type = "image";  
   } else  
   {  
     type = "*";  
   }  
   type += "/*";  
   return type;  
 }