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官方技术文档翻译——Gradle 插件用户指南(7)
  2. Android Studio项目应该提交哪些文件到GitHub上
  3. Android下文件操作模式(含SDCard的读写)
  4. JNI基础实验一:调用.so文件--友善之臂Tiny210 android 串口/pwm/A
  5. Android媒体文件metadata是如何实现跟解码器结合获取的
  6. Android 读取U盘或SD卡中的所有.txt文件
  7. gif文件导出png

随机推荐

  1. 进度条及拖动条背景颜色设置(progressDraw
  2. Android(安卓)windowSoftInputMode属性解
  3. android---------ndk中的各个版本的下载
  4. Android(安卓)中文 API (100) —— Scrol
  5. android intent的常用方法
  6. ANDROID轻量级JSON序列化和反序列化[转]
  7. Android(安卓)WebView总结
  8. Android(安卓)读写文件的N种写法
  9. android 按钮Button单击背景切换
  10. android 用VideoView播放本地视频文件