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