Android文件关联

Android 文件关联详解2010-09-10 09:42这是网上被转载比较多的关于的描述:

首先说一下,AndroidManifest.xml文件:4 t% |. w" }, l1 p- m: V+ u0 B& H D
<?xml version="1.0" encoding="UTF-8"?>
<manifest android:versionCode="1" android:versionName="1.0"
package="com.openfiledialog" xmlns:android="http://schemas.android.com/apk/res/android">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:label="@string/app_name" android:name=".MainActivity">
<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="*/*"></data>
</intent-filter>
</activity>
<activity android:name=".FileList"/>

</application>
<uses-sdk android:minSdkVersion="7"/>
</manifest>
& o# Y- i, O+ j( l# }+ d
注意,这里面有两组“intent-filter”。这里,实现文件关联主要是第二组在起作用。
k! Y2 U/ Z4 X) v( q“android:mimeType="*/*"”表示所有文件类型都和这个apk连接起来。只要在资源管理器中点击任何文件,都会弹出窗口问你用哪个软件打开,而其中必有本apk。
# a8 L+ K! r% E! I. W" `- P9 h如果写成“android:mimeType="image/jpg"”,则只关联jpg文件。

public class MainActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Intent intent = getIntent();
String action = intent.getAction();
if(intent.ACTION_VIEW.equals(action)){
TextView tv = (TextView)findViewById(R.id.tvText);
tv.setText(intent.getDataString());
}
}
}
注意其中的“ if(intent.ACTION_VIEW.equals(action))”,这里的ACTION_VIEW和前面的“android.intent.action.VIEW”是对应的。" i; t* e6 D8 J3 U- a% V" N: L
这里可选项很多,我就不一一介绍了,有兴趣的读者请自行察看相关文档——不是我不想介绍,而是因为我也不懂……' l2 e4 m, R% {2 l7 n3 x3 }
[4 U3 A ]6 i x: T
然后,通过“intent.getDataString()”就可以获取传进来的参数,也就是点击的文件名(包含路径,而且以file://开头)。

问题是我不明白我看完之后还是不明白怎么关联一般的文件,因为不明白什么是MIME类型。在强大的搜索帮助下终于找到关于MIME类型的详细解释:http://en.wikipedia.org/wiki/MIME_type#List_of_common_media_types这是WIKI上关于MIME类型的说明很详细。
举个例子如果我们想关联pdf文档直接将上面代码中<data android:mimeType="*/*"></data>中的"*/*"替换成"application/pdf"j即可。

另外,WIKI上还有一篇文章值得一读:http://en.wikipedia.org/wiki/URI_scheme。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值