index.uts 5.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
/* 引入 interface.uts 文件中定义的变量 */
import { ChooseSystemImage, ChooseSystemImageOptions, ChooseSystemImageSuccessResult, ChooseSystemVideo, ChooseSystemVideoOptions, ChooseSystemVideoSuccessResult } from '../interface.uts';
import AppCompatActivity from 'androidx.appcompat.app.AppCompatActivity';
import ActivityResultCallback from 'androidx.activity.result.ActivityResultCallback';
import List from 'kotlin.collections.List';
import Uri from 'android.net.Uri';
import ActivityResultContracts from 'androidx.activity.result.contract.ActivityResultContracts';
import ActivityResultLauncher from 'androidx.activity.result.ActivityResultLauncher';
import PickVisualMediaRequest from "androidx.activity.result.PickVisualMediaRequest";
import Builder from "androidx.activity.result.PickVisualMediaRequest.Builder";
import Context from 'com.alibaba.fastjson.parser.deserializer.ASMDeserializerFactory.Context';
import MediaStore from 'android.provider.MediaStore';
import Activity from "android.app.Activity"
import Intent from 'android.content.Intent';
import ChooseSystemImageActivity from "uts.sdk.modules.uniChooseSystemImage.ChooseSystemImageActivity"

/* 引入 unierror.uts 文件中定义的变量 */
import { ImageErrorImpl } from '../unierror';
import ChooseVideoOptions from 'uts.sdk.modules.DCloudUniMedia.ChooseVideoOptions';
import BitmapFactory from 'android.graphics.BitmapFactory';
import File from 'java.io.File';
import FileInputStream from 'java.io.FileInputStream';
import FileOutputStream from 'java.io.FileOutputStream';
import InputStream from 'java.io.InputStream';
import Build from 'android.os.Build';
var resultCallback : ((requestCode : Int, resultCode : Int, data ?: Intent) => void) | null = null

export const chooseSystemImage : ChooseSystemImage = function (option : ChooseSystemImageOptions) {
	if (option.count <= 0) {
		var error = new ImageErrorImpl(2101002, "uni-chooseSystemImage")
		option.fail?.(error)
		option.complete?.(error)
		return
	}
	if (Build.VERSION.SDK_INT > 32) {
		__chooseSystemImage(option)
	} else {
		UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, [android.Manifest.permission.READ_EXTERNAL_STORAGE], (a : boolean, b : string[]) => {
			__chooseSystemImage(option)
		}, (a : boolean, b : string[]) => {
			var error = new ImageErrorImpl(2101005, "uni-chooseSystemImage")
			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!.getStringArrayExtra("paths")
					if (result != null && result!.size > 0) {
						var paths : Array<string> = []
						result.forEach((p : string) => {
							if (UTSAndroid.isUniAppX()) {
								paths.push("file://" + (p))
							} else {
								paths.push("file://" + copyResource(p))
							}

						})
						var success : ChooseSystemImageSuccessResult = {
							filePaths: paths
						}
						option.success?.(success)
						option.complete?.(success)
					} else {
						var error = new ImageErrorImpl(2101001, "uni-chooseSystemImage")
						option.fail?.(error)
						option.complete?.(error)
					}
				} else {
					var error = new ImageErrorImpl(2101001, "uni-chooseSystemImage")
					option.fail?.(error)
					option.complete?.(error)
				}
			} else {
				var error = new ImageErrorImpl(2101001, "uni-chooseSystemImage")
				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)
		intent.putExtra("type", 1)
		UTSAndroid.getUniActivity()!.startActivityForResult(intent, 10086)
	} catch (e) {
		var error = new ImageErrorImpl(2101010, "uni-chooseSystemImage")
		option.fail?.(error)
		option.complete?.(error)
	}
}

var CACHEPATH = UTSAndroid.getAppCachePath()
function copyResource(url : string) : string {
	var path : String = CACHEPATH!
	if (CACHEPATH?.endsWith("/") == true) {
		path = CACHEPATH + "/uni-getSystemImage/"
	} else {
		path = CACHEPATH + "/uni-getSystemImage/"
	}
	path = path + new File(url).getName()
	console.log(path)
	copyFile(url, path)
	return path
}

function copyFile(fromFilePath : string, toFilePath : string) : boolean {
	var fis : InputStream | null = null
	try {
		let fromFile = new File(fromFilePath)
		if (!fromFile.exists()) {
			return false;
		}
		if (!fromFile.isFile()) {
			return false
		}
		if (!fromFile.canRead()) {
			return false;
		}
		fis = new FileInputStream(fromFile);
		if (fis == null) {
			return false
		}
	} catch (e) {
		return false;
	}
	let toFile = new File(toFilePath)
	if (!toFile.getParentFile().exists()) {
		toFile.getParentFile().mkdirs()
	}
	if (!toFile.exists()) {
		toFile.createNewFile()
	}
	try {
		let fos = new FileOutputStream(toFile)
		let byteArrays = ByteArray(1024)
		var c = fis!!.read(byteArrays)
		while (c > 0) {
			fos.write(byteArrays, 0, c)
			c = fis!!.read(byteArrays)
		}
		fis!!.close()
		fos.close()
		return true
	} catch (e) {
		return false;
	}
}