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。

更多相关文章

  1. Android(安卓)pm命令详解
  2. android 中apk如何防止反编译?
  3. android tips:从资源文件中读取文件流显示
  4. Android(安卓)dp方式的屏幕适配工具使用(Android(安卓)Studio插
  5. Android(安卓)emulator中的system.img,userdata.img和ramdisk.img
  6. android中常见的错误及解决办法
  7. 在Android中实现文件读写
  8. 使用Android(安卓)Studio打包指定名称的Apk
  9. NPM 和webpack 的基础使用

随机推荐

  1. 如何实现 Android(安卓)ListView『上拉加
  2. Android(安卓)MVP 框架 Demo
  3. android studio如何生成library project(
  4. Binder机制总结
  5. android之service简单介绍
  6. Android(安卓)studio 常用插件合集
  7. 几个比较混淆概念,android中的Info类以及P
  8. AndroidManifest.xml文件综合详解
  9. GridLayout 使用
  10. [转]微信ANDROID客户端-会话速度提升70%