Flex Mobile拍照并保存在自定义目录

开发Flex Mobile应用时经常需要拍照,拍照后照片会自动保存在指定目录中,但不同设备自动保存的路径是不同的,开发人员希望将拍摄的照片保存在自定义的路径中,下面的代码实现了该功能。

(1)页面布局文件。页面布局非常简单,只有一个按钮。


(2)在按钮的单击事件中调用拍照。

//The click method of taking photo button.
protected function btnTakePhoto_clickHandler(event:MouseEvent):void
{
	if(CameraUI.isSupported)
	{
		//Open the view of taking photo.
		var camera:CameraUI=new CameraUI();
		camera.launch(MediaType.IMAGE);
		//Add the complete handler of CameraUI.
		camera.addEventListener(MediaEvent.COMPLETE,onTakePhotoCompleteHandler);
	}
}

(3)拍照完成后进行自定义保存

//Define the object of MeidaPromise and IDataInput.
private var mediaPromise:MediaPromise;
private var fileDataSource:IDataInput;
			
//The handler of taking photo complete.
private function onTakePhotoCompleteHandler(event:MediaEvent):void
{
	mediaPromise=event.data as MediaPromise;
	mediaPromise.addEventListener(Event.COMPLETE,mediaPromiseCompleteHandler);
	fileDataSource=mediaPromise.open();
}
			
//The handler of MeidaPromise complete.
private function mediaPromiseCompleteHandler(event:Event):void
{
	//Remove the handler of MediaPromise complete.
	mediaPromise.removeEventListener(Event.COMPLETE,mediaPromiseCompleteHandler);
				
	//Write the bytes to the ByteArray.
	var fileData:ByteArray=new ByteArray();
	fileDataSource.readBytes(fileData,0,fileDataSource.bytesAvailable);
				
	//Write the photo data to the File.
	var file:File=new File("/mnt/sdcard/DCIM/"+mediaPromise.relativePath);
	var fileStream:FileStream=new FileStream();
	fileStream.open(file,FileMode.WRITE);
	fileStream.writeBytes(fileData);
	mediaPromise.close();
	fileStream.close();
}

本人亲测通过,希望有所帮助。

你可能感兴趣的:(Flex Mobile拍照并保存在自定义目录)