H5Plus/MUI FAQ
指定应用可运行的 Android 最低版本
http://ask.dcloud.net.cn/article/193
获取本地图片的 BASE64 编码
这里的 zoomImage() 是我自己写的压缩图片的方法,忽略就好
js
plus.gallery.pick(
function (path) {
var newPath = "_download/" + new Date().getTime() + ".jpg";
var maxWidth = T.getMaxWidth();
var bitmap = new plus.nativeObj.Bitmap("test");
var wait = plus.nativeUI.showWaiting("正在压缩图片。..");
zoomImage(path, newPath, maxWidth, function () {
wait.close();
var dst = plus.io.convertLocalFileSystemURL(newPath);
bitmap.load(dst, function () {
var base4 = bitmap.toBase64Data();
console.log(base4.split(",")[1]);
T._imgs.push(base4.split(",")[1]);
T.$emit("upload_end", T._imgs.join(","));
});
});
},
function (e) {
console.log("取消选择图片");
},
{ filter: "image" }
);
打包时地理定位之类的 key(如百度地图)
sh
BA:AD:09:3A:82:82:9F:B4:32:A7:B2:8C:B4:CC:F0:E9:F3:7D:AE:58
io.dcloud.H5...
应用升级
js
function upgrade(url) {
plus.nativeUI.showWaiting("升级中。..");
var dtask = plus.downloader.createDownload(
url,
{
method: "GET",
},
function (d, status) {
if (status == 200) {
console.log("下载升级包成功:" + d.filename);
plus.runtime.install(
d.filename,
{},
function () {
plus.nativeUI.closeWaiting();
plus.nativeUI.alert("更新成功,需要重新启动!", function () {
plus.runtime.restart();
});
},
function (e) {
plus.nativeUI.closeWaiting();
alert("更新失败:" + e.message);
}
);
} else {
plus.nativeUI.closeWaiting();
alert("下载升级包失败:" + status);
}
}
);
// dtask.addEventListener('statechanged', function(d, status) {
// console.log("statechanged: " + d.state);
// });
dtask.start();
}
图片压缩
js
// 压缩图片
function compressImage(src, dst, quality, callback) {
plus.zip.compressImage(
{
src: src,
dst: dst,
quality: quality,
},
function () {
console.log("Compress success!");
callback();
},
function (error) {
console.log("Compress error!");
}
);
}
// 缩放图片
function zoomImage(src, dst, width, callback) {
plus.zip.compressImage(
{
src: src,
dst: dst,
width: width,
},
function () {
console.log("Compress success!");
callback();
},
function (error) {
console.log("Compress error!" + JSON.stringify(error));
}
);
}
文件上传
js
/**
* 上传文件
* @param {Object} url
* @param {Object} filePath
*/
function upload(url, filePath) {
var task = plus.uploader.createUpload(
serverUrl + "/" + url,
{
method: "POST",
},
function (t, status) {
// 上传完成
if (status == 200) {
alert("Upload success: " + t.url);
} else {
plus.nativeUI.toast("Upload failed: " + status);
}
}
);
task.addFile(filePath, {
key: "testdoc",
});
//task.addData( "string_key", "string_value" );
//task.addEventListener( "statechanged", onStateChanged, false );
task.start();
}
锁定屏幕方向
js
// 竖屏
plus.screen.lockOrientation("portrait-primary");
获取图片的 BASE64
js
function GetBase64Code(path) {
//path 绝对路径
var bitmap = new plus.nativeObj.Bitmap("test"); //test 标识谁便取
// 从本地加载 Bitmap 图片
bitmap.load(
path,
function () {
var base4 = bitmap.toBase64Data();
var datastr = base4.split(",", 3);
if (datastr.length > 1) {
pics.push(datastr[1]);
} else {
pics.push(datastr[0]);
}
console.log("加载图片:" + base4);
},
function (e) {
console.log("加载图片失败:" + JSON.stringify(e));
}
);
}
利用 native.js 保持 app 后台运行
如果不想程序后台被杀死,可以使用这个方法。
js
var g_wakelock = null;
// 允许程序后台运行,以持续获取 GPS 位置
function wakeLock() {
//Android
debugger;
var main = plus.android.runtimeMainActivity();
var Context = plus.android.importClass("android.content.Context");
var PowerManager = plus.android.importClass("android.os.PowerManager");
var pm = main.getSystemService(Context.POWER_SERVICE);
g_wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ANY_NAME");
g_wakelock.acquire();
}
// 结束程序后台运行
function releaseWakeLock() {
if (g_wakelock != null && g_wakelock.isHeld()) {
g_wakelock.release();
g_wakelock = null;
}
}