head.addEventListener('tap', function() { var IMAGE_UNSPECIFIED = "image/*"; //相册显示的文件类型 var PHOTOZOOM = 2; // 获取完图片返回key var PHOTOLAT = 1; // 剪裁完毕后返回key var main = plus.android.runtimeMainActivity(); //h5+获取应用主Activity实例对象 var Intent = plus.android.importClass("android.content.Intent"); //导入java类intent对象 以下importClass都是使用安卓原生类 var MediaStore = plus.android.importClass("android.provider.MediaStore"); var File = plus.android.importClass("java.io.File"); var Uri = plus.android.importClass("android.net.Uri"); var intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED); intent.putExtra("scale", true); intent.putExtra("scaleUpIfNeeded", true); var outPutPath = plus.io.convertLocalFileSystemURL("_www/head.jpg"); main.startActivityForResult(intent, PHOTOZOOM); main.onActivityResult = function(requestCode, resultCode, data) { if (PHOTOZOOM == requestCode) { var file = new File(outPutPath); // 输出目录uri var outPutUri = Uri.fromFile(file); if (data == undefined) { return false; } plus.android.importClass(data); var uri = data.getData(); var cropIntent = new Intent("com.android.camera.action.CROP"); //裁剪 cropIntent.setDataAndType(uri, IMAGE_UNSPECIFIED); // 截图完毕后 输出目录 cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, outPutUri); cropIntent.putExtra("crop", "true"); //aspectX aspectY 是宽高的比例 cropIntent.putExtra("aspectX", 1); cropIntent.putExtra("aspectY", 1); // outputX outputY 是裁剪图片宽高 cropIntent.putExtra("outputX", 70); cropIntent.putExtra("outputY", 70); cropIntent.putExtra("return-data", true); main.startActivityForResult(cropIntent, PHOTOLAT); } else if (requestCode == PHOTOLAT) { if (data == undefined) { return false; } var a = new File(outPutPath); if (!a.exists()) { console.log(outPutPath); mui.toast('裁剪错误'); return false; } img.src = outPutPath; } } });
参考文章 http://ask.dcloud.net.cn/question/8314
来源:https://www.cnblogs.com/fangdong/p/6893317.html