基于安全考虑,Flash只允许将数据写入系统剪贴板中,因此,Flash之中只能执行System下的setClipboard方法.
基于Flash的:
例子如下:
使用AS将数据写入内存
-
package?{ ??
-
????import?flash.display.Sprite; ??
-
????import?flash.system.System; ??
-
??
-
????public?class?SystemExample?extends?Sprite?{ ??
-
????????public?function?SystemExample()?{ ??
-
????????????System.setClipboard("写入内存的数据"); ??
-
????????} ??
-
????} ??
- }??
但是,在AIR中会自由多了,允许读取与写入,但也不是没妈管的孩子,什么事都能做.
在AIR中能读取与写入的有六种数据格式,都由ClipboardFormats来撑管,我们看看这六种数据格式
BITMAP_FORMAT:图像数据。
FILE_LIST_FORMAT:文件数组。
HTML_FORMAT :HTML 数据。
RICH_TEXT_FORMAT:RTF 格式数据。
TEXT_FORMAT:字符串数据。
URL_FORMAT:URL 字符串。
看个由HELP文档中挖出来的例子吧,作用是向剪贴板写入一个随机数:
AS3代码
-
import?flash.desktop.ClipboardFormats; ??
-
? ??
-
?Clipboard.generalClipboard.setDataHandler(ClipboardFormats.TEXT_FORMAT,?randomNumberGenerator); ??
-
??
-
?public?function?randomNumberGenerator():String{ ??
-
?????return?Math.random().toString(); ??
- ?}??
跟据多种数据格式做分类处理,以下是写来获取剪贴板图像源的类文件,将图像源转化为图片.
忘了说明一点,这是用在Flex中的,所以toBitmap中返加UIComponent类型
AS3代码
-
?
-
?
-
?
-
?
-
??
-
package?com.flashNote.getData ??
-
{ ??
-
????????import?flash.desktop.Clipboard; ??
-
????????import?flash.desktop.ClipboardFormats; ??
-
????????import?flash.display.Bitmap; ??
-
????????import?flash.display.BitmapData; ??
-
???????? ??
-
????????import?mx.core.UIComponent; ??
-
????????public?class?GetBoardData ??
-
????????{ ??
-
????????private?static?var?Instance:GetBoardData=new?GetBoardData; ??
-
????????public?static?function?getInstance():GetBoardData{ ??
-
????????????return?Instance; ??
-
????????}? ??
-
????????public?function?GetBoardData() ??
-
????????{ ??
-
???????????? ??
-
????????} ??
-
????????public?function?getData():BoardFormat{ ??
-
????????????var?returnObj:BoardFormat; ??
-
????????????returnObj.sort=getSelFormat(); ??
-
????????????returnObj.data=Clipboard.generalClipboard.getData(returnObj.sort) ??
-
????????????var?bmd:BitmapData?=returnObj.data?as?BitmapData; ??
-
????????????toBitmap(bmd) ??
-
????????????return?returnObj; ??
-
????????} ??
-
????????private?function?toBitmap(bmd:BitmapData):UIComponent{ ??
-
????????????var?ui:UIComponent=new?UIComponent; ??
-
????????????var?bm:Bitmap?=?new?Bitmap(bmd); ??
-
????????????ui.addChild(bm); ??
-
????????????return?ui; ??
-
????????} ??
-
????????private?function?getSelFormat():String{ ??
-
????????????var?backStr:String; ??
-
????????????backStr=getFormat(ClipboardFormats.BITMAP_FORMAT) ??
-
????????????if(backStr!=""){ ??
-
????????????????return?backStr; ??
-
????????????} ??
-
????????????backStr=getFormat(ClipboardFormats.FILE_LIST_FORMAT) ??
-
????????????if(backStr!=""){ ??
-
????????????????return?backStr; ??
-
????????????} ??
-
????????????backStr=getFormat(ClipboardFormats.HTML_FORMAT) ??
-
????????????if(backStr!=""){ ??
-
????????????????return?backStr; ??
-
????????????} ??
-
????????????backStr=getFormat(ClipboardFormats.TEXT_FORMAT) ??
-
????????????if(backStr!=""){ ??
-
????????????????return?backStr; ??
-
????????????} ??
-
????????????backStr=getFormat(ClipboardFormats.URL_FORMAT) ??
-
????????????if(backStr!=""){ ??
-
????????????????return?backStr; ??
-
????????????} ??
-
????????????function?getFormat(str:String):String{ ??
-
????????????????if(Clipboard.generalClipboard.hasFormat(str)){ ??
-
????????????????????return?str ??
-
????????????????}else{ ??
-
????????????????????return?""; ??
-
????????????????} ??
-
????????????} ??
-
????????????return?null; ??
-
????????} ??
-
????} ??
- }??
?
AS3代码
-
?
-
?
-
?
-
?
-
??
-
package?com.flashNote.getData ??
-
{ ??
-
????public?class?BoardFormat ??
-
????{ ??
-
????????public?var?sort:String; ??
-
????????public?var?data:Object; ??
-
????} ??
- }??