提交 e02013bb 编写于 作者: 张磊

支持chooseSystemMadia

上级 c93683eb
## 1.1.0(2024-10-31)
新增chooseSystemMedia,支持选择图片和视频。
## 1.0.0(2024-10-23)
新增插件
{ {
"id": "uni-chooseSystemImage", "id": "uni-chooseSystemImage",
"displayName": "uni-chooseSystemImage", "displayName": "uni-chooseSystemMedia",
"version": "1.0.0", "version": "1.1.0",
"description": "uni-chooseSystemImage", "description": "从手机相册中选择图片或视频,解决google play新政策禁止添加媒体权限的问题",
"keywords": [ "keywords": [
"uni-chooseSystemImage" "google",
"上架",
"图片选择"
], ],
"repository": "", "repository": "",
"engines": { "engines": {
"HBuilderX": "^3.6.8" "HBuilderX": "^4.29"
}, },
"dcloudext": { "dcloudext": {
"type": "uts", "type": "uts",
...@@ -24,9 +26,9 @@ ...@@ -24,9 +26,9 @@
"qq": "" "qq": ""
}, },
"declaration": { "declaration": {
"ads": "", "ads": "",
"data": "", "data": "本插件不会采集任何隐私信息,获取权限仅是为了兼容android12及以下版本的系统。",
"permissions": "" "permissions": "<uses-permission android:name=\"android.permission.READ_EXTERNAL_STORAGE\" />"
}, },
"npmurl": "" "npmurl": ""
}, },
...@@ -35,47 +37,47 @@ ...@@ -35,47 +37,47 @@
"encrypt": [], "encrypt": [],
"platforms": { "platforms": {
"cloud": { "cloud": {
"tcb": "u", "tcb": "y",
"aliyun": "u", "aliyun": "y",
"alipay": "u" "alipay": "y"
}, },
"client": { "client": {
"Vue": { "Vue": {
"vue2": "u", "vue2": "n",
"vue3": "u" "vue3": "n"
}, },
"App": { "App": {
"app-android": "u", "app-android": "u",
"app-ios": "u", "app-ios": "n",
"app-harmony": "u" "app-harmony": "n"
}, },
"H5-mobile": { "H5-mobile": {
"Safari": "u", "Safari": "n",
"Android Browser": "u", "Android Browser": "n",
"微信浏览器(Android)": "u", "微信浏览器(Android)": "n",
"QQ浏览器(Android)": "u" "QQ浏览器(Android)": "n"
}, },
"H5-pc": { "H5-pc": {
"Chrome": "u", "Chrome": "n",
"IE": "u", "IE": "n",
"Edge": "u", "Edge": "n",
"Firefox": "u", "Firefox": "n",
"Safari": "u" "Safari": "n"
}, },
"小程序": { "小程序": {
"微信": "u", "微信": "n",
"阿里": "u", "阿里": "n",
"百度": "u", "百度": "n",
"字节跳动": "u", "字节跳动": "n",
"QQ": "u", "QQ": "n",
"钉钉": "u", "钉钉": "n",
"快手": "u", "快手": "n",
"飞书": "u", "飞书": "n",
"京东": "u" "京东": "n"
}, },
"快应用": { "快应用": {
"华为": "u", "华为": "n",
"联盟": "u" "联盟": "n"
} }
} }
} }
......
/* 引入 interface.uts 文件中定义的变量 */ /* 引入 interface.uts 文件中定义的变量 */
import { ChooseSystemImage, ChooseSystemImageOptions, ChooseSystemImageSuccessResult, ChooseSystemVideo, ChooseSystemVideoOptions, ChooseSystemVideoSuccessResult } from '../interface.uts'; import { ChooseSystemImage, ChooseSystemImageOptions, ChooseSystemImageSuccessResult, ChooseSystemMedia, ChooseSystemMediaOptions, ChooseSystemMediaSuccessResult, ChooseSystemVideo, ChooseSystemVideoOptions, ChooseSystemVideoSuccessResult } from '../interface.uts';
import AppCompatActivity from 'androidx.appcompat.app.AppCompatActivity'; import AppCompatActivity from 'androidx.appcompat.app.AppCompatActivity';
import ActivityResultCallback from 'androidx.activity.result.ActivityResultCallback'; import ActivityResultCallback from 'androidx.activity.result.ActivityResultCallback';
import List from 'kotlin.collections.List'; import List from 'kotlin.collections.List';
...@@ -23,6 +23,9 @@ import FileInputStream from 'java.io.FileInputStream'; ...@@ -23,6 +23,9 @@ import FileInputStream from 'java.io.FileInputStream';
import FileOutputStream from 'java.io.FileOutputStream'; import FileOutputStream from 'java.io.FileOutputStream';
import InputStream from 'java.io.InputStream'; import InputStream from 'java.io.InputStream';
import Build from 'android.os.Build'; import Build from 'android.os.Build';
import Parcelable from 'android.os.Parcelable';
import Media from 'uts.sdk.modules.uniChooseSystemImage.Media';
var resultCallback : ((requestCode : Int, resultCode : Int, data ?: Intent) => void) | null = null var resultCallback : ((requestCode : Int, resultCode : Int, data ?: Intent) => void) | null = null
export const chooseSystemImage : ChooseSystemImage = function (option : ChooseSystemImageOptions) { export const chooseSystemImage : ChooseSystemImage = function (option : ChooseSystemImageOptions) {
...@@ -45,22 +48,132 @@ export const chooseSystemImage : ChooseSystemImage = function (option : ChooseSy ...@@ -45,22 +48,132 @@ export const chooseSystemImage : ChooseSystemImage = function (option : ChooseSy
} }
} }
function __chooseSystemImage(option : ChooseSystemImageOptions) { export const chooseSystemMedia : ChooseSystemMedia = function (option : ChooseSystemMediaOptions) {
if (option.count <= 0) {
var error = new ImageErrorImpl(2101002, "uni-chooseSystemMedia")
option.fail?.(error)
option.complete?.(error)
return
}
if (option.count > 100) {
option.count = 100
}
if (Build.VERSION.SDK_INT > 32) {
__chooseSystemMedia(option)
} else {
UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, [android.Manifest.permission.READ_EXTERNAL_STORAGE], (a : boolean, b : string[]) => {
__chooseSystemMedia(option)
}, (a : boolean, b : string[]) => {
var error = new ImageErrorImpl(2101005, "uni-chooseSystemMedia")
option.fail?.(error)
option.complete?.(error)
})
}
}
function __chooseSystemMedia(option : ChooseSystemMediaOptions) {
try { try {
resultCallback = (requestCode : Int, resultCode : Int, data : Intent | null) => { resultCallback = (requestCode : Int, resultCode : Int, data : Intent | null) => {
UTSAndroid.offAppActivityResult(resultCallback!) UTSAndroid.offAppActivityResult(resultCallback!)
if (10086 == requestCode && resultCode == -1) { if (10086 == requestCode && resultCode == -1) {
if (data != null) { if (data != null) {
var result = data!.getStringArrayExtra("paths") var result = data!.getParcelableArrayExtra("paths")
if (result != null && result!.size > 0) { if (result != null && result!.size > 0) {
var paths : Array<string> = [] var paths : Array<string> = []
result.forEach((p : string) => { result.forEach((p : Parcelable) => {
if (UTSAndroid.isUniAppX()) { if (p instanceof Media)
paths.push("file://" + (p)) if (UTSAndroid.isUniAppX()) {
} else { paths.push("file://" + (p.path!))
paths.push("file://" + copyResource(p)) } else {
} if (p.type == 1) {
paths.push("file://" + copyResource(p.path!))
} else {
paths.push("file://" + (p.path!))
}
}
})
var success : ChooseSystemMediaSuccessResult = {
filePaths: paths
}
option.success?.(success)
option.complete?.(success)
} else {
var error = new ImageErrorImpl(2101001, "uni-chooseSystemMedia")
option.fail?.(error)
option.complete?.(error)
}
} else {
var error = new ImageErrorImpl(2101001, "uni-chooseSystemMedia")
option.fail?.(error)
option.complete?.(error)
}
} else {
var error = new ImageErrorImpl(2101001, "uni-chooseSystemMedia")
option.fail?.(error)
option.complete?.(error)
}
}
UTSAndroid.onAppActivityResult(resultCallback!)
var intent = new Intent(UTSAndroid.getUniActivity()!, Class.forName("uts.sdk.modules.uniChooseSystemImage.ChooseSystemImageActivity"))
intent.putExtra("count", option.count)
if (option.mediaType != null) {
if (option.mediaType!.indexOf("mix") >= 0) {
intent.putExtra("type", 3)
} else if (option.mediaType!.indexOf("image") >= 0) {
intent.putExtra("type", 1)
} else if (option.mediaType!.indexOf("video") >= 0) {
intent.putExtra("type", 2)
} else {
intent.putExtra("type", 1)
}
}
switch (option.pageOrientation) {
case "auto": {
intent.putExtra("page_orientation", 2)
break
}
case "portrait": {
intent.putExtra("page_orientation", 1)
break
}
case "landscape": {
intent.putExtra("page_orientation", 0)
break
}
default: {
intent.putExtra("page_orientation", 1)
break
}
}
UTSAndroid.getUniActivity()!.startActivityForResult(intent, 10086)
} catch (e) {
var error = new ImageErrorImpl(2101010, "uni-chooseSystemMedia")
option.fail?.(error)
option.complete?.(error)
}
}
function __chooseSystemImage(option : ChooseSystemImageOptions) {
try {
resultCallback = (requestCode : Int, resultCode : Int, data : Intent | null) => {
UTSAndroid.offAppActivityResult(resultCallback!)
if (10086 == requestCode && resultCode == -1) {
if (data != null) {
var result = data!.getParcelableArrayExtra("paths")
if (result != null && result!.size > 0) {
var paths : Array<string> = []
result.forEach((p : Parcelable) => {
if (p instanceof Media)
if (UTSAndroid.isUniAppX()) {
paths.push("file://" + (p.path!))
} else {
if (p.type == 1) {
paths.push("file://" + copyResource(p.path!))
} else {
paths.push("file://" + (p.path!))
}
}
}) })
var success : ChooseSystemImageSuccessResult = { var success : ChooseSystemImageSuccessResult = {
filePaths: paths filePaths: paths
...@@ -99,12 +212,11 @@ var CACHEPATH = UTSAndroid.getAppCachePath() ...@@ -99,12 +212,11 @@ var CACHEPATH = UTSAndroid.getAppCachePath()
function copyResource(url : string) : string { function copyResource(url : string) : string {
var path : String = CACHEPATH! var path : String = CACHEPATH!
if (CACHEPATH?.endsWith("/") == true) { if (CACHEPATH?.endsWith("/") == true) {
path = CACHEPATH + "/uni-getSystemImage/" path = CACHEPATH + "uni-getSystemMedia/"
} else { } else {
path = CACHEPATH + "/uni-getSystemImage/" path = CACHEPATH + "/uni-getSystemMedia/"
} }
path = path + new File(url).getName() path = path + new File(url).getName()
console.log(path)
copyFile(url, path) copyFile(url, path)
return path return path
} }
......
...@@ -17,4 +17,22 @@ export type ChooseSystemImageOptions = { ...@@ -17,4 +17,22 @@ export type ChooseSystemImageOptions = {
complete ?: ChooseSystemImageCompleteCallback | null complete ?: ChooseSystemImageCompleteCallback | null
} }
export type ChooseSystemImage = (options : ChooseSystemImageOptions) => void export type ChooseSystemImage = (options : ChooseSystemImageOptions) => void
\ No newline at end of file
export type ChooseSystemMediaSuccessResult = {
filePaths : Array<string>
}
export type ChooseSystemMediaSuccessCallback = (result : ChooseSystemMediaSuccessResult) => void
export type ChooseSystemMediaFailResult = ChooseSystemImageError
export type ChooseSystemMediaFailCallback = (result : ChooseSystemMediaFailResult) => void
export type ChooseSystemMediaCompleteCallback = (callback : any) => void
export type ChooseSystemMediaOptions = {
count : number,
mediaType ?: Array<string> | null,
pageOrientation ?: string | null,
success ?: ChooseSystemMediaSuccessCallback | null,
fail ?: ChooseSystemMediaFailCallback | null,
complete ?: ChooseSystemMediaCompleteCallback | null
}
export type ChooseSystemMedia = (options : ChooseSystemMediaOptions) => void
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册