index.uts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* 引入 interface.uts 文件中定义的变量 */
  2. import { ChooseSystemImage, ChooseSystemImageOptions, ChooseSystemImageSuccessResult, ChooseSystemMedia, ChooseSystemMediaOptions, ChooseSystemMediaSuccessResult, ChooseSystemVideo, ChooseSystemVideoOptions, ChooseSystemVideoSuccessResult } from '../interface.uts';
  3. import AppCompatActivity from 'androidx.appcompat.app.AppCompatActivity';
  4. import ActivityResultCallback from 'androidx.activity.result.ActivityResultCallback';
  5. import List from 'kotlin.collections.List';
  6. import Uri from 'android.net.Uri';
  7. import ActivityResultContracts from 'androidx.activity.result.contract.ActivityResultContracts';
  8. import ActivityResultLauncher from 'androidx.activity.result.ActivityResultLauncher';
  9. import PickVisualMediaRequest from "androidx.activity.result.PickVisualMediaRequest";
  10. import Builder from "androidx.activity.result.PickVisualMediaRequest.Builder";
  11. import Context from 'com.alibaba.fastjson.parser.deserializer.ASMDeserializerFactory.Context';
  12. import MediaStore from 'android.provider.MediaStore';
  13. import Activity from "android.app.Activity"
  14. import Intent from 'android.content.Intent';
  15. import ChooseSystemImageActivity from "uts.sdk.modules.uniChooseSystemImage.ChooseSystemImageActivity"
  16. /* 引入 unierror.uts 文件中定义的变量 */
  17. import { ImageErrorImpl } from '../unierror';
  18. import ChooseVideoOptions from 'uts.sdk.modules.DCloudUniMedia.ChooseVideoOptions';
  19. import BitmapFactory from 'android.graphics.BitmapFactory';
  20. import File from 'java.io.File';
  21. import FileInputStream from 'java.io.FileInputStream';
  22. import FileOutputStream from 'java.io.FileOutputStream';
  23. import InputStream from 'java.io.InputStream';
  24. import Build from 'android.os.Build';
  25. import Parcelable from 'android.os.Parcelable';
  26. import Media from 'uts.sdk.modules.uniChooseSystemImage.Media';
  27. var resultCallback : ((requestCode : Int, resultCode : Int, data ?: Intent) => void) | null = null
  28. export const chooseSystemImage : ChooseSystemImage = function (option : ChooseSystemImageOptions) {
  29. if (option.count <= 0) {
  30. var error = new ImageErrorImpl(2101002, "uni-chooseSystemImage")
  31. option.fail?.(error)
  32. option.complete?.(error)
  33. return
  34. }
  35. if (Build.VERSION.SDK_INT > 32 || UTSAndroid.getUniActivity()!.applicationInfo.targetSdkVersion >= 33) {
  36. __chooseSystemImage(option)
  37. } else {
  38. UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, [android.Manifest.permission.READ_EXTERNAL_STORAGE], (a : boolean, b : string[]) => {
  39. __chooseSystemImage(option)
  40. }, (a : boolean, b : string[]) => {
  41. var error = new ImageErrorImpl(2101005, "uni-chooseSystemImage")
  42. option.fail?.(error)
  43. option.complete?.(error)
  44. })
  45. }
  46. }
  47. export const chooseSystemMedia : ChooseSystemMedia = function (option : ChooseSystemMediaOptions) {
  48. if (option.count <= 0) {
  49. var error = new ImageErrorImpl(2101002, "uni-chooseSystemMedia")
  50. option.fail?.(error)
  51. option.complete?.(error)
  52. return
  53. }
  54. if (option.count > 100) {
  55. option.count = 100
  56. }
  57. if (Build.VERSION.SDK_INT > 32 || UTSAndroid.getUniActivity()!.applicationInfo.targetSdkVersion >= 33) {
  58. __chooseSystemMedia(option)
  59. } else {
  60. UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, [android.Manifest.permission.READ_EXTERNAL_STORAGE], (a : boolean, b : string[]) => {
  61. __chooseSystemMedia(option)
  62. }, (a : boolean, b : string[]) => {
  63. var error = new ImageErrorImpl(2101005, "uni-chooseSystemMedia")
  64. option.fail?.(error)
  65. option.complete?.(error)
  66. })
  67. }
  68. }
  69. function __chooseSystemMedia(option : ChooseSystemMediaOptions) {
  70. try {
  71. resultCallback = (requestCode : Int, resultCode : Int, data : Intent | null) => {
  72. UTSAndroid.offAppActivityResult(resultCallback!)
  73. if (10086 == requestCode && resultCode == -1) {
  74. if (data != null) {
  75. var result = data!.getParcelableArrayExtra("paths")
  76. if (result != null && result!.size > 0) {
  77. var paths : Array<string> = []
  78. result.forEach((p : Parcelable) => {
  79. if (p instanceof Media)
  80. if (UTSAndroid.isUniAppX()) {
  81. paths.push("file://" + (p.path!))
  82. } else {
  83. if (p.type == 1) {
  84. paths.push("file://" + copyResource(p.path!))
  85. } else {
  86. paths.push("file://" + (p.path!))
  87. }
  88. }
  89. })
  90. var success : ChooseSystemMediaSuccessResult = {
  91. filePaths: paths
  92. }
  93. option.success?.(success)
  94. option.complete?.(success)
  95. } else {
  96. var error = new ImageErrorImpl(2101001, "uni-chooseSystemMedia")
  97. option.fail?.(error)
  98. option.complete?.(error)
  99. }
  100. } else {
  101. var error = new ImageErrorImpl(2101001, "uni-chooseSystemMedia")
  102. option.fail?.(error)
  103. option.complete?.(error)
  104. }
  105. } else {
  106. var error = new ImageErrorImpl(2101001, "uni-chooseSystemMedia")
  107. option.fail?.(error)
  108. option.complete?.(error)
  109. }
  110. }
  111. UTSAndroid.onAppActivityResult(resultCallback!)
  112. var intent = new Intent(UTSAndroid.getUniActivity()!, Class.forName("uts.sdk.modules.uniChooseSystemImage.ChooseSystemImageActivity"))
  113. intent.putExtra("count", option.count)
  114. if (option.mediaType != null) {
  115. if (option.mediaType!.indexOf("mix") >= 0) {
  116. intent.putExtra("type", 3)
  117. } else if (option.mediaType!.indexOf("image") >= 0) {
  118. intent.putExtra("type", 1)
  119. } else if (option.mediaType!.indexOf("video") >= 0) {
  120. intent.putExtra("type", 2)
  121. } else {
  122. intent.putExtra("type", 1)
  123. }
  124. }
  125. switch (option.pageOrientation) {
  126. case "auto": {
  127. intent.putExtra("page_orientation", 2)
  128. break
  129. }
  130. case "portrait": {
  131. intent.putExtra("page_orientation", 1)
  132. break
  133. }
  134. case "landscape": {
  135. intent.putExtra("page_orientation", 0)
  136. break
  137. }
  138. default: {
  139. intent.putExtra("page_orientation", 1)
  140. break
  141. }
  142. }
  143. UTSAndroid.getUniActivity()!.startActivityForResult(intent, 10086)
  144. } catch (e) {
  145. var error = new ImageErrorImpl(2101010, "uni-chooseSystemMedia")
  146. option.fail?.(error)
  147. option.complete?.(error)
  148. }
  149. }
  150. function __chooseSystemImage(option : ChooseSystemImageOptions) {
  151. try {
  152. resultCallback = (requestCode : Int, resultCode : Int, data : Intent | null) => {
  153. UTSAndroid.offAppActivityResult(resultCallback!)
  154. if (10086 == requestCode && resultCode == -1) {
  155. if (data != null) {
  156. var result = data!.getParcelableArrayExtra("paths")
  157. if (result != null && result!.size > 0) {
  158. var paths : Array<string> = []
  159. result.forEach((p : Parcelable) => {
  160. if (p instanceof Media)
  161. if (UTSAndroid.isUniAppX()) {
  162. paths.push("file://" + (p.path!))
  163. } else {
  164. if (p.type == 1) {
  165. paths.push("file://" + copyResource(p.path!))
  166. } else {
  167. paths.push("file://" + (p.path!))
  168. }
  169. }
  170. })
  171. var success : ChooseSystemImageSuccessResult = {
  172. filePaths: paths
  173. }
  174. option.success?.(success)
  175. option.complete?.(success)
  176. } else {
  177. var error = new ImageErrorImpl(2101001, "uni-chooseSystemImage")
  178. option.fail?.(error)
  179. option.complete?.(error)
  180. }
  181. } else {
  182. var error = new ImageErrorImpl(2101001, "uni-chooseSystemImage")
  183. option.fail?.(error)
  184. option.complete?.(error)
  185. }
  186. } else {
  187. var error = new ImageErrorImpl(2101001, "uni-chooseSystemImage")
  188. option.fail?.(error)
  189. option.complete?.(error)
  190. }
  191. }
  192. UTSAndroid.onAppActivityResult(resultCallback!)
  193. var intent = new Intent(UTSAndroid.getUniActivity()!, Class.forName("uts.sdk.modules.uniChooseSystemImage.ChooseSystemImageActivity"))
  194. intent.putExtra("count", option.count)
  195. intent.putExtra("type", 1)
  196. UTSAndroid.getUniActivity()!.startActivityForResult(intent, 10086)
  197. } catch (e) {
  198. var error = new ImageErrorImpl(2101010, "uni-chooseSystemImage")
  199. option.fail?.(error)
  200. option.complete?.(error)
  201. }
  202. }
  203. var CACHEPATH = UTSAndroid.getAppCachePath()
  204. function copyResource(url : string) : string {
  205. var path : String = CACHEPATH!
  206. if (CACHEPATH?.endsWith("/") == true) {
  207. path = CACHEPATH + "uni-getSystemMedia/"
  208. } else {
  209. path = CACHEPATH + "/uni-getSystemMedia/"
  210. }
  211. path = path + new File(url).getName()
  212. copyFile(url, path)
  213. return path
  214. }
  215. function copyFile(fromFilePath : string, toFilePath : string) : boolean {
  216. var fis : InputStream | null = null
  217. try {
  218. let fromFile = new File(fromFilePath)
  219. if (!fromFile.exists()) {
  220. return false;
  221. }
  222. if (!fromFile.isFile()) {
  223. return false
  224. }
  225. if (!fromFile.canRead()) {
  226. return false;
  227. }
  228. fis = new FileInputStream(fromFile);
  229. if (fis == null) {
  230. return false
  231. }
  232. } catch (e) {
  233. return false;
  234. }
  235. let toFile = new File(toFilePath)
  236. if (!toFile.getParentFile().exists()) {
  237. toFile.getParentFile().mkdirs()
  238. }
  239. if (!toFile.exists()) {
  240. toFile.createNewFile()
  241. }
  242. try {
  243. let fos = new FileOutputStream(toFile)
  244. let byteArrays = ByteArray(1024)
  245. var c = fis!!.read(byteArrays)
  246. while (c > 0) {
  247. fos.write(byteArrays, 0, c)
  248. c = fis!!.read(byteArrays)
  249. }
  250. fis!!.close()
  251. fos.close()
  252. return true
  253. } catch (e) {
  254. return false;
  255. }
  256. }