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


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

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

<activityandroid:name=".EasyNote"android:label="@string/app_name"android:launchMode="singleTask"android:screenOrientation="portrait">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<actionandroid:name="android.intent.action.VIEW"></action>
<categoryandroid:name="android.intent.category.DEFAULT"></category>
<dataandroid: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(MicrosoftWord文件)

message/rfc822(RFC822形式)

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

更多相关文章

  1. 一款常用的 Squid 日志分析工具
  2. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  3. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  4. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  5. Android中widget编写注意事项——1(程序成功执行Done却没有widget
  6. 我的Android(安卓)NDK之旅(一),不使用ndk-build命令来创建jni
  7. Android(安卓)重学系列 fence原理
  8. Android的dex热修复的实现基本原理
  9. [Android][Android(安卓)Studio] Gradle项目中添加JNI生成文件(.

随机推荐

  1. sqlserver not in 语句使程充崩溃
  2. 解决SQL Server的“此数据库没有有效所有
  3. SQL建立数据库及删除数据库命令
  4. SQLServer 全文检索(full-text)语法
  5. sqlserver isnull在数据库查询中的应用
  6. SQL 比较一个集合是否在另一个集合里存在
  7. 动态给表添加删除字段并同时修改它的插入
  8. SQL对时间处理的语句小结
  9. sqlserver 数据库学习笔记
  10. SQL中exists的使用方法