AIR管理文件关联, 指定文件默认打开方式

参考:http://help.adobe.com/zh_CN/AIR/1.1/devappsflex/WS5b3ccc516d4fbf351e63e3d118676a5f3b-8000.html#WS5b3ccc516d4fbf351e63e3d118666ade46-7e18

 

对于文件关联管理,air中的flash.desktop.NativeApplication类提供了4个方法:

isSetAsDefaultApplication(extension:String):Boolean
返回当前AIR应用程序是否是指定文件格式的默认打开方式
参数extension是文件扩展名字符串,不用写”.”,比如”flv”,下面3条的extension一样

setAsDefaultApplication(extension:String):void
使当前应用程序与某种文件格式关联

removeAsDefaultApplication(extension:String):void
移除某个AIR应用程序与文件之间的关联

getDefaultApplication(extension:String):String
报告某个文件所关联的应用程序路径
返回应用程序路径的字符串

文件关联必须要在应用描述文件中声明。当AIR应用程序安装时,运行环境会自动关联相应文件,设置相应格式的文件默认打开方式为你的AIR应用程序。不过,因为AIR安装程序不覆盖任何文件关联,所以已经有别的程序设置为默认打开方式的文件格式是不处理的。
有个良好的习惯做法是:在程序启动时核实你所希望关联的文件格式,也就是核实是否已设置默认打开方式为用该AIR应用程序。毕竟AIR应用程序不覆盖已存在的文件关联,而且别的应用程序可能将你的文件关联修改。如果遇到别的安装程序关联了你需要关联的文件,可以使用setAsDefaultApplication()设置关联,不过最好先得到用户的同意哦.
需要注意的是,AIR只允许管理在应用描述文件中声明了扩展名的文件关联,所以,我们是无法获取未声明的文件类型的关联信息的,而且对所有未声明的扩展名做操作都会导致抛出程序异常。
在应用描述文件中声明文件关联的元素结构如下:

<fileTypes>
<fileType>
<name>adobe.VideoFile</name>
<extension>avf</extension>
<description>Adobe Video File</description>
<contentType>application/vnd.adobe.video-file</contentType>
<icon>
<image16×16>icons/AIRApp_16.png</image16×16>
<image32×32>icons/AIRApp_32.png</image32×32>
<image48×48>icons/AIRApp_48.png</image48×48>
<image128×128>icons/AIRApp_128.png</image128×128>
</icon>
</fileType>
</fileTypes>

更多关于应用描述文件请见此文——AIR应用描述文件详解

[09.08.31更新]
小新同学:上面那个例子已经很清晰了呀,再贴一个官方的AIR的例子:

 

 
 
  1. <fileTypes> 
  2.             <fileType> 
  3.                 <name>com.adobe.flv</name> 
  4.                 <extension>flv</extension> 
  5.                 <description>Video for Flash</description> 
  6.                 <contentType>video/x-flv</contentType> 
  7.                 <icon> 
  8.                     <image16x16>/assets/icons/flv_icon_16.png</image16x16> 
  9.                     <image32x32>/assets/icons/flv_icon_32.png</image32x32> 
  10.                     <image48x48>/assets/icons/flv_icon_48.png</image48x48> 
  11.                     <image128x128>/assets/icons/flv_icon_128.png</image128x128> 
  12.                 </icon> 
  13.             </fileType> 
  14.             <fileType> 
  15.                 <name>com.adobe.f4v</name> 
  16.                 <extension>f4v</extension> 
  17.                 <description>MPEG-4 Video for Flash</description> 
  18.                 <contentType>video/x-f4v</contentType> 
  19.                 <icon> 
  20.                     <image16x16>/assets/icons/f4v_icon_16.png</image16x16> 
  21.                     <image32x32>/assets/icons/f4v_icon_32.png</image32x32> 
  22.                     <image48x48>/assets/icons/f4v_icon_48.png</image48x48> 
  23.                     <image128x128>/assets/icons/f4v_icon_128.png</image128x128> 
  24.                 </icon> 
  25.             </fileType> 
  26.         </fileTypes> 

猜你喜欢

转载自luoke920.iteye.com/blog/1223005