保存图片到手机相册功能插件

通过保存图片到系统相册插件,js可以把网络上或者是app文件夹内或者base64后的图片保存到相册,实现一些类似下载图片,保存二维码之类的功能

1.保存到相册js函数

imageURL是要被保存的图片地址,successFun是保存成功后执行的函数,failFun是保存失败时执行的函数

lbuilder.Native.saveImage(imageURL,successFun,failFun);

2.imageURL图片地址说明和要求

a.如果图片是服务器上的必须是http或者https开头的完整的地址,不能是相对地址,支持png和jpg图片

b.如果图片地址是www目录里面的,也就是图片是被打包在上传的代码中的,则地址是相对地址,例如在www文件夹下有个logo.png 文件,则代码样式是

lbuilder.Native.saveImage("logo.png", function(message){}, function(err){});

c.如果图片是base64格式的则直接传入图片的base64数据,也就是通常的img标签的src属性的值

3.简单的保存图片到相册的代码例子

lbuilder.Native.saveImage("http://www.lbuilder.com/index/assets/images/logo.png", function(message){
	alert("save success");
}, function(err){
	alert("save fail"+err);
});

4.完整的保存图片到相册的例子

<!DOCTYPE>
<html>
	<head>
		<title>lbuilder demo</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"/>
		<meta http-equiv="Cache-Control" content="no-siteapp"/>
	</head>
	<body>
		<h3>Lbuilder demo</h3>
		<a href="javascript:lbuilder.Native.saveImage('http://www.lbuilder.com/index/assets/images/logo.png', function(message){alert(message);}, function(err){alert(err);});" >save image</a><br/><br/>
		<a href="javascript:lbuilder.Native.saveImage('game.png', function(message){alert(message);}, function(err){alert(err);});" >save game.png in www folder</a><br/><br/>
		<a href="javascript:lbuilder.Native.saveImage('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAARCAYAAAArDQkmAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA3FpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM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n944Ar5LcAAjR/0Lg+Z6p4AAAAASUVORK5CYII=', function(message){alert(message);}, function(err){alert(err);});" >save base64 image</a><br/><br/>
	</body>
</html>