1.创建uts项目
2.创建uts插件cf-takepic
3.在index.uts中编写原生安卓代码,首先定义一个函数方法,在页面中看是否可引用成功
uts函数代码
/*** 拍照函数*/
export const takepic=function():void{console.log("11111111")
}
index.vue代码
<template><view class="content"><view class="text-area"><button @click="take">拍照</button></view></view>
</template><script>//引入uts插件import {takepic} from "../../uni_modules/cf-takepic"export default {data() {return {}},onLoad() {},methods: {take(){//调用uts插件函数takepic()}}}
</script><style>.content {display: flex;flex-direction: column;align-items: center;justify-content: center;}</style>
4.运行到手机或模拟器,成功打印111111即可视为调用uts插件成功
5.修改uts插件takepic函数,原生android唤起手机相机功能
uts代码
/*** 引用 Android 系统库,示例如下:* import { Context } from "android.content.Context";* [可选实现,按需引入]*//* 引入 interface.uts 文件中定义的变量 */
import { MyApiOptions, MyApiResult, MyApi, MyApiSync } from '../interface.uts';/* 引入 unierror.uts 文件中定义的变量 */
import { MyApiFailImpl } from '../unierror';
import Intent from "android.content.Intent";
import MediaStore from "android.provider.MediaStore";/*** 更多插件开发的信息详见:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html*/
/*** 拍照函数*/
export const takepic=function():void{console.log("11111111")let takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);UTSAndroid.getUniActivity()!.startActivityForResult(takePictureIntent, 999);UTSAndroid.onAppActivityResult((requestCode : Int, resultCode : Int, data ?: Intent) => {if (requestCode == 999) {console.log("拍照回调");} else {// 别的代码发起的请求,不要处理}});}
拍照打印日志
6.再次修改uts代码,获取图片路径
uts代码
/*** 引用 Android 系统库,示例如下:* import { Context } from "android.content.Context";* [可选实现,按需引入]*//* 引入 interface.uts 文件中定义的变量 */
import { MyApiOptions, MyApiResult, MyApi, MyApiSync } from '../interface.uts';/* 引入 unierror.uts 文件中定义的变量 */
import { MyApiFailImpl } from '../unierror';
import Intent from "android.content.Intent";
import MediaStore from "android.provider.MediaStore";
import File from "java.io.File";
import SimpleDateFormat from "java.text.SimpleDateFormat";
import Environment from "android.os.Environment";
import Uri from "android.net.Uri";
import FileProvider from "androidx.core.content.FileProvider";
import ActivityCompat from "androidx.core.app.ActivityCompat";
import Manifest from "android.Manifest";
import PackageManager from "android.content.pm.PackageManager";/*** 更多插件开发的信息详见:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html*/
/*** 拍照函数*/
export const takepic = function () : void {// 检查相关权限是否已经具备if (ActivityCompat.checkSelfPermission(UTSAndroid.getUniActivity()!, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {// 不具备权限,申请权限,并且告知用户监听失败ActivityCompat.requestPermissions(UTSAndroid.getUniActivity()!, arrayOf(Manifest.permission.CAMERA), 1002)return;}console.log("11111111")let takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);let photoFile = createImageFile();console.log(photoFile)let photoUri = FileProvider.getUriForFile(UTSAndroid.getAppContext()!,"com.cf.take",photoFile as File); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri as Uri);UTSAndroid.getUniActivity()!.startActivityForResult(takePictureIntent, 999);UTSAndroid.onAppActivityResult((requestCode : Int, resultCode : Int, data ?: Intent) => {if (requestCode == 999) {console.log("拍照回调");console.log(photoUri.toString());} else {// 别的代码发起的请求,不要处理}});}export const createImageFile = function () : File {let timeStamp = "123456789";let imageFileName = "JPEG_" + timeStamp + "_";let storageDir = UTSAndroid.getAppContext()!.getExternalFilesDir(Environment.DIRECTORY_PICTURES);console.log(storageDir,"storageDir")let image = File.createTempFile(imageFileName, /* prefix */".jpg", /* suffix */storageDir /* directory */);
console.log(image,"image")return image as File;
}
日志