App调用Android设备本地相机拍照并保存到本地相册

cameraBtn.addEventListener(MouseEvent.CLICK,cameraClickHandler);

protected function cameraClickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
//先绘制当前图行到一个bigmapdata对象
var bmpData:BitmapData = new BitmapData(GameModel.getInstance().screenWidth,GameModel.getInstance().screenHeight);
bmpData.draw(this);

if(CameraRoll.supportsAddBitmapData)
{
//测试环境是平板是会走到这里
// creatTxt();
var roll:CameraRoll = new CameraRoll();
roll.addEventListener(Event.COMPLETE,onCreatePicComlete);
roll.addEventListener(ErrorEvent.ERROR, onCreatePicError);
roll.addBitmapData(bmpData);
}
else
{
//测试环境是虚拟机里是会走到这里

//利用pngEncoder解码器开始对bitmapdata编码成二进制数据ByteArray
var bytes:ByteArray = PNGEncoder.encode(bmpData);

//把二进制数据写入到本地中

//根据当前系统时间来编辑图片文件的名字
var now:Date = new Date();
var year:Number=now.getFullYear();//年
var month:Number=now.getMonth()+1;//月
var day:Number=now.getDate();//日

var hour:Number = now.getHours();//时
var min:Number = now.getMinutes();//分
var sec:Number = now.getSeconds();//秒

var fileName:String = "word_" + year + month + day + hour + min + sec;
var fileFullName:String = File.applicationStorageDirectory.resolvePath("img/"+ fileName +".png").nativePath;
var file:File = new File(fileFullName);
trace(file.nativePath);
var stream:FileStream = new FileStream();
stream.open(file,FileMode.WRITE);
stream.writeBytes(bytes);
stream.close();
}
}

protected function onCreatePicError(event:ErrorEvent):void
{
// TODO Auto-generated method stub
trace("相册图片创建失败");
// creatTxt();
var roll:CameraRoll = CameraRoll(event.target);
roll.removeEventListener(Event.COMPLETE,onCreatePicComlete);
roll.removeEventListener(ErrorEvent.ERROR, onCreatePicError);
}

protected function onCreatePicComlete(event:Event):void
{
// TODO Auto-generated method stub
trace("相册图片创建成功");
var roll:CameraRoll = CameraRoll(event.target);
roll.removeEventListener(Event.COMPLETE,onCreatePicComlete);
roll.removeEventListener(ErrorEvent.ERROR, onCreatePicError);

creatTxt();
ZoeDelay.Delay(1500,remoeTxtHanler);
}

private function creatTxt():void
{
savePicREsultTxt = new TextField();
var format:TextFormat = new TextFormat();
format.size = 60;
format.align = TextFormatAlign.CENTER;
format.color = 0x000000;
format.bold = true;
savePicREsultTxt.selectable = false;
savePicREsultTxt.mouseEnabled = false;
savePicREsultTxt.text = "圖片保存成功!";
// savePicREsultTxt.text = "創建圖片!";
savePicREsultTxt.width = 500;
savePicREsultTxt.height = 80;

savePicREsultTxt.setTextFormat(format);
savePicREsultTxt.x = (GameModel.getInstance().screenWidth - savePicREsultTxt.width)/2;
savePicREsultTxt.y = (GameModel.getInstance().screenHeight - savePicREsultTxt.height)/2;
addChild(savePicREsultTxt);
}

private var savePicREsultTxt:TextField;

private function remoeTxtHanler():void
{
// TODO Auto Generated method stub
this.removeChild(savePicREsultTxt);
savePicREsultTxt = null;
}

 

如果要通过App直接访问手机的本地相册,roll.browseForImage();就解决了

posted on 2013-05-29 10:11  橙小风  阅读(6145)  评论(0编辑  收藏  举报