index.uts 20.2 KB
Newer Older
1 2
import { getUniActivity } from "io.dcloud.uts.android";
import {
DCloud-yyl's avatar
DCloud-yyl 已提交
3 4 5 6 7 8 9 10 11 12
	ChooseImageOptions, ChooseImage,
	PreviewImageOptions, PreviewImage, PreviewImageSuccess,
	ClosePreviewImage, ClosePreviewImageSuccess, ClosePreviewImageOptions,
	GetImageInfo, GetImageInfoOptions, GetImageInfoSuccess,
	SaveImageToPhotosAlbum, SaveImageToPhotosAlbumOptions, SaveImageToPhotosAlbumSuccess,
	CompressImage, CompressImageOptions,
	ChooseVideo, ChooseVideoOptions,
	GetVideoInfo, GetVideoInfoOptions, GetVideoInfoSuccess,
	SaveVideoToPhotosAlbum, SaveVideoToPhotosAlbumOptions, SaveVideoToPhotosAlbumSuccess,
	CompressVideo, CompressVideoOptions,
13 14 15
} from "../interface.uts"
import {
	UniError_PreviewImage, UniError_GetImageInfo, UniError_SaveImageToPhotosAlbum, UniError_SaveVideoToPhotosAlbum,
DCloud-yyl's avatar
DCloud-yyl 已提交
16
	MediaErrorImpl
17 18 19
} from "../unierror.uts"
import { chooseMediaImage, chooseMediaVideo } from "./utils/ChooseMediaUtils.uts"
import { transcodeImage, transcodeVideo } from './utils/CompressUtils.uts'
DCloud-yyl's avatar
DCloud-yyl 已提交
20
import { getVideoMetadata, copyFile } from "./utils/MediaUtils.uts"
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
import Intent from 'android.content.Intent';
import Manifest from 'android.Manifest';
import Build from 'android.os.Build';
import File from 'java.io.File';
import Uri from 'android.net.Uri';
import ArrayList from 'java.util.ArrayList';

import Glide from 'com.bumptech.glide.Glide';
import CustomTarget from 'com.bumptech.glide.request.target.CustomTarget';
import Drawable from 'android.graphics.drawable.Drawable';
import Transition from 'com.bumptech.glide.request.transition.Transition';
import ExifInterface from 'android.media.ExifInterface';
import BitmapFactory from 'android.graphics.BitmapFactory';
import TextUtils from 'android.text.TextUtils';
import FileInputStream from 'java.io.FileInputStream';
import FileOutputStream from 'java.io.FileOutputStream';
import Environment from 'android.os.Environment';
import LongClickEventManager from 'io.dcloud.uts.nativeObj.photoview.LongClickEventManager';
import JSONObject from 'org.json.JSONObject';
import ActivityManager from 'io.dcloud.uts.nativeObj.photoview.ActivityManager';
import MediaMetadataRetriever from 'android.media.MediaMetadataRetriever';
import MediaStore from 'android.provider.MediaStore';
import ContentValues from 'android.content.ContentValues';
import Exception from 'java.lang.Exception';
DCloud-yyl's avatar
DCloud-yyl 已提交
45 46
import InputStream from 'java.io.InputStream';
import BufferedInputStream from 'java.io.BufferedInputStream';
47 48 49
// import { actionSheetImpl, ShowActionSheetOptions, closeActionSheet } from "./showActionSheet.uts";


DCloud-yyl's avatar
DCloud-yyl 已提交
50 51 52
const mediaCachePath = UTSAndroid.getAppCachePath() + "uni-media/"

const ASSETS_PATH = '/android_asset/'
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
/**
 * 图片压缩目前采用原java代码压缩方式
 */


export const chooseImage : ChooseImage = function (option : ChooseImageOptions) {
	chooseMediaImage(option)
}

export const previewImage : PreviewImage = function (options : PreviewImageOptions) {
	if (options.urls.length > 0) {
		let values : ArrayList<String> = new ArrayList()
		let oroginalValues : ArrayList<String> = new ArrayList()
		options.urls.forEach((original) => {
			let localPath = original
			if (!localPath.startsWith("data:image") && !isNetPath(localPath)) {
				localPath = UTSAndroid.convert2AbsFullPath(localPath)
			}
DCloud-yyl's avatar
DCloud-yyl 已提交
71 72 73 74
			if (localPath.startsWith(ASSETS_PATH)) {
				localPath = localPath.replace(ASSETS_PATH, "")
			}
			
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
			values.add(localPath)
			oroginalValues.add(original)
		})
		let intent = new Intent()
		intent.setClassName(getUniActivity()!, "io.dcloud.uts.nativeObj.photoview.PhotoActivity");
		intent.putExtra("image_urlList", values);
		intent.putExtra("original_image_urlArray", oroginalValues)
		var current = 0;
		if (options.current != null) {
			try {
				current = (options.current!.toString()).toInt()
				if (current < 0) {
					current = 0
				}
			} catch (e) {

			}
		}
		intent.putExtra("image_current_index", current)
		intent.putExtra("image_loop", options.loop != null ? options.loop! : false);
		if (options.indicator != null) {
			intent.putExtra("image_indicator", options.indicator!);
		}
		intent.putExtra("image_photo", true);
		var callbackId = System.currentTimeMillis() + ":"
		intent.putExtra("preview_callback", callbackId);
		// uts目前无法支持嵌套方法,长按事件暂时不支持
		// LongClickEventManager.getInstance().addOnlongClickListener(callbackId, new LongClick(options))
		getUniActivity()!.startActivity(intent)
DCloud-yyl's avatar
DCloud-yyl 已提交
104
		let success : PreviewImageSuccess = { errMsg: 'ok', "errSubject": UniError_PreviewImage }
105 106 107
		options.success?.(success)
		options.complete?.(success)
	} else {
DCloud-yyl's avatar
DCloud-yyl 已提交
108
		let error = new MediaErrorImpl(1101002, UniError_PreviewImage);
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
		options.fail?.(error)
		options.complete?.(error)
	}
}

class LongClick implements LongClickEventManager.OnLongClickListener {
	options : PreviewImageOptions
	constructor(options : PreviewImageOptions) {
		this.options = options
	}
	override onLongClickListener(values : JSONObject) {
		var itemList : Array<string> | null = null
		var itemColor : string | null = null
		var hasLongPressAction = false;
		if (this.options.longPressActions != null && this.options.longPressActions!.itemList != null && this.options.longPressActions!.itemList!.length > 0) {
			itemList = this.options.longPressActions!.itemList!
			hasLongPressAction = true
		} else {
			itemList = ["保存"]
		}
		if (this.options.longPressActions != null && this.options.longPressActions!.itemColor != null) {
			itemColor = this.options.longPressActions!.itemColor!
		}
		// let actionOption : ShowActionSheetOptions = {
		// 	"itemList": itemList!,
		// 	"itemColor": itemColor == null ? "" : itemColor!,
		// 	success: (e) => {
		// 		if (hasLongPressAction) {
		// 			let success : LongPressActionsSuccessData = {
		// 				"index": values.optInt("index"),
		// 				"tapIndex": e.tapIndex!
		// 			}
		// 			this.options.longPressActions?.success?.(success)
		// 			this.options.longPressActions?.complete?.(success)
		// 			return
		// 		}
		// 		let saveOption : SaveImageToPhotosAlbumOptions = {
		// 			"filePath": values.optString("url"),
		// 			success: (e) => {
		// 				uni.showToast({
		// 					title: "uni.previewImage.save.success",
		// 					position: "bottom"
		// 				})
		// 			},
		// 			fail: (e) => {
		// 				uni.showToast({
		// 					title: "uni.previewImage.save.fail",
		// 					position: "bottom"
		// 				})
		// 			},
		// 			complete: (e) => {
		// 			}
		// 		}
		// 		loadFile(saveOption, false)
		// 	},
		// 	fail: (e) => {
		// 		if (hasLongPressAction) {
		// 			let fail = {
		// 				"errMsg": "user cancel"
		// 			}
		// 			this.options.longPressActions?.fail?.(fail)
		// 			this.options.longPressActions?.complete?.(fail)
		// 		}
		// 	}
		// }
		// let activity = ActivityManager.getInstance().getRunningActivity("io.dcloud.uts.nativeObj.photoview.PhotoActivity")
		// actionSheetImpl(activity, actionOption)
		// uni.showActionSheet()
	}
}

function isNetPath(url : string) : boolean {
	if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("rtmp://") || url.startsWith("rtsp://")) {
		return true;
	}
	return false;
}

export const closePreviewImage : ClosePreviewImage = function (options : ClosePreviewImageOptions) {
	// 可能存在执行关闭的时候 action sheet 正在显示
	// closeActionSheet()
	let activity = ActivityManager.getInstance().getRunningActivity("io.dcloud.uts.nativeObj.photoview.PhotoActivity")
	if (activity != null) {
		activity.onBackPressed()
	}
DCloud-yyl's avatar
DCloud-yyl 已提交
194
	let callback : ClosePreviewImageSuccess = {
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
		errMsg: "ok"
	}
	options.success?.(callback)
	options.complete?.(callback)
}

export const getImageInfo : GetImageInfo = function (options : GetImageInfoOptions) {
	if (isNetPath(options.src)) {
		Glide.with(getUniActivity()!).asFile().load(options.src).into(new ImageInfoTarget(options))
	} else {
		var path = ""
		if (options.src.length > 0) {
			path = UTSAndroid.convert2AbsFullPath(options.src)
			getExif(path, options, null)
		} else {
DCloud-yyl's avatar
DCloud-yyl 已提交
210
			let imageInfoCallback = new MediaErrorImpl(1101003, UniError_GetImageInfo);
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
			options.fail?.(imageInfoCallback)
			options.complete?.(imageInfoCallback)
		}
	}
}

class ImageInfoTarget extends CustomTarget<File> {
	options : GetImageInfoOptions

	constructor(options : GetImageInfoOptions) {
		super();
		this.options = options
	}

	override onResourceReady(resource : File, transition : Transition<KotlinVarianceAnnotationIn<File>> | null) {
		let bitmapOption = new BitmapFactory.Options();
		bitmapOption.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(resource.getPath(), bitmapOption)
		let mimeType : string = bitmapOption.outMimeType
		if (!TextUtils.isEmpty(mimeType) && mimeType.contains("/")) {
			mimeType = mimeType.substring(mimeType.indexOf("/") + 1)
		}
		// 拼接保存路径
DCloud-yyl's avatar
DCloud-yyl 已提交
234
		let path = mediaCachePath + System.currentTimeMillis()
235 236 237 238 239 240 241 242
		if (!TextUtils.isEmpty(mimeType)) {
			path += "." + mimeType
		} else {
			path += ".jpg"
		}
		if (copyFile(resource.getPath(), path)) {
			getExif(path, this.options, bitmapOption)
		} else {
DCloud-yyl's avatar
DCloud-yyl 已提交
243
			let imageInfoCallback = new MediaErrorImpl(1101004, UniError_GetImageInfo);
244 245 246 247 248 249 250 251 252
			this.options.fail?.(imageInfoCallback)
			this.options.complete?.(imageInfoCallback)
		}
	}

	override onLoadCleared(placeholder : Drawable | null) {
	}

	override onLoadFailed(errorDrawable : Drawable | null) {
DCloud-yyl's avatar
DCloud-yyl 已提交
253
		let imageInfoCallback = new MediaErrorImpl(1101004, UniError_GetImageInfo);
254 255 256 257 258 259
		this.options.fail?.(imageInfoCallback)
		this.options.complete?.(imageInfoCallback)
	}
}

function getExif(src : string, options : GetImageInfoOptions, bitmapOption : BitmapFactory.Options | null) {
DCloud-yyl's avatar
DCloud-yyl 已提交
260
	var exifInfo : ExifInterface | null = null
DCloud-yyl's avatar
DCloud-yyl 已提交
261 262 263 264
	if(bitmapOption == null) {
		bitmapOption = new BitmapFactory.Options();
		bitmapOption.inJustDecodeBounds = true;
	}
DCloud-yyl's avatar
DCloud-yyl 已提交
265 266
	if (src.startsWith(ASSETS_PATH)) {
		exifInfo = new ExifInterface(UTSAndroid.getUniActivity()!.getAssets().open(src.replace(ASSETS_PATH, "")));
DCloud-yyl's avatar
DCloud-yyl 已提交
267
		BitmapFactory.decodeStream(UTSAndroid.getUniActivity()!.getAssets().open(src.replace(ASSETS_PATH, "")), null, bitmapOption)
DCloud-yyl's avatar
DCloud-yyl 已提交
268 269
	} else if (new File(src).exists()) {
		exifInfo = new ExifInterface(src)
DCloud-yyl's avatar
DCloud-yyl 已提交
270
		BitmapFactory.decodeFile(src, bitmapOption)
DCloud-yyl's avatar
DCloud-yyl 已提交
271 272
	} else {
		let imageInfoCallback = new MediaErrorImpl(1101003, UniError_GetImageInfo);
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
		options.fail?.(imageInfoCallback)
		options.complete?.(imageInfoCallback)
		return
	}
	try {
		let width = bitmapOption!.outWidth
		let height = bitmapOption!.outHeight
		let mimeType : string = bitmapOption!.outMimeType
		if (!TextUtils.isEmpty(mimeType) && mimeType.contains("/")) {
			mimeType = mimeType.substring(mimeType.indexOf("/") + 1)
		}
		let orientation = exifInfo.getAttribute(ExifInterface.TAG_ORIENTATION);
		let orientationStr = ""
		switch (orientation) {
			case "2":
				orientationStr = "up-mirrored";
				break;
			case "3":
				orientationStr = "down";
				break;
			case "4":
				orientationStr = "down-mirrored";
				break;
			case "5":
				orientationStr = "left-mirrored";
				break;
			case "6":
				orientationStr = "right";
				break;
			case "7":
				orientationStr = "right-mirrored";
				break;
			case "8":
				orientationStr = "left";
				break;
			case "0":
			case "1":
			default:
				orientationStr = "up";
				break;
		}
DCloud-yyl's avatar
DCloud-yyl 已提交
314
		let imageInfoCallback : GetImageInfoSuccess = {
315 316 317 318 319 320 321 322
			"width": width,
			"height": height,
			"path": "file://" + src,
			"orientation": orientationStr,
			"type": TextUtils.isEmpty(mimeType) ? "unknown" : mimeType
		}
		options.success?.(imageInfoCallback)
		options.complete?.(imageInfoCallback)
DCloud-yyl's avatar
DCloud-yyl 已提交
323 324
	} catch (e : Exception) {
		let imageInfoCallback = new MediaErrorImpl(1101010, UniError_GetImageInfo);
325 326 327 328 329 330 331 332 333 334 335
		options.fail?.(imageInfoCallback)
		options.complete?.(imageInfoCallback)
	}
}

export const saveImageToPhotosAlbum : SaveImageToPhotosAlbum = function (options : SaveImageToPhotosAlbumOptions) {
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
		let requestPermissionList : Array<string> = [Manifest.permission.WRITE_EXTERNAL_STORAGE]
		UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, requestPermissionList, (a : boolean, b : string[]) => {
			loadFile(options, true)
		}, (a : boolean, b : string[]) => {
DCloud-yyl's avatar
DCloud-yyl 已提交
336
			let error = new MediaErrorImpl(1101005, UniError_SaveImageToPhotosAlbum);
337 338 339 340 341 342 343 344 345 346 347 348 349
			options.fail?.(error)
			options.complete?.(error)
		})
	} else {
		loadFile(options, true)
	}
}

function loadFile(options : SaveImageToPhotosAlbumOptions, saveToAlbum : boolean) {
	if (isNetPath(options.filePath)) {
		Glide.with(getUniActivity()!).asFile().load(options.filePath).into(new SaveToAlbumTarget(options, saveToAlbum))
	} else {
		if (TextUtils.isEmpty(options.filePath)) {
DCloud-yyl's avatar
DCloud-yyl 已提交
350
			let error = new MediaErrorImpl(1101003, UniError_SaveImageToPhotosAlbum);
351 352 353 354 355
			options.fail?.(error)
			options.complete?.(error)
			return
		}
		let originalPath = UTSAndroid.convert2AbsFullPath(options.filePath)
DCloud-yyl's avatar
DCloud-yyl 已提交
356 357
		if (originalPath.startsWith(ASSETS_PATH)) { } else if (!new File(originalPath).exists()) {
			let error = new MediaErrorImpl(1101003, UniError_SaveImageToPhotosAlbum);
358 359 360 361 362 363 364 365 366
			options.fail?.(error)
			options.complete?.(error)
			return
		}
		let path = DCIM_PATH + originalPath.substring(originalPath.lastIndexOf("/") + 1)
		if (copyFileToPublicPath(originalPath, path, false)) {
			if (saveToAlbum) {
				sendSaveToAlbumBroad(path)
			}
DCloud-yyl's avatar
DCloud-yyl 已提交
367
			let success : SaveImageToPhotosAlbumSuccess = {
368 369 370 371 372
				"path": path
			}
			options.success?.(success)
			options.complete?.(success)
		} else {
DCloud-yyl's avatar
DCloud-yyl 已提交
373
			let error = new MediaErrorImpl(1101006, UniError_SaveImageToPhotosAlbum);
374 375 376 377 378 379
			options.fail?.(error)
			options.complete?.(error)
		}
	}
}

DCloud-yyl's avatar
DCloud-yyl 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392 393
function getInputStream(fromFilePath:string):InputStream|null {
	var fis : InputStream | null = null
	try {
	if (fromFilePath.startsWith(ASSETS_PATH)) {
		fis = UTSAndroid.getUniActivity()!.getAssets().open(fromFilePath.replace(ASSETS_PATH, ""));
	} else {
		fis = new FileInputStream(fromFilePath)
	}
	} catch(e){
		
	}
	return fis
}

394 395 396 397 398 399 400 401 402 403
function copyFileToPublicPath(fromFilePath : string, toFilePath : string, isVideo : boolean) : boolean {
	if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
		try {
			return copyFile(fromFilePath, toFilePath)
		} catch (e) {
			return false
		}
	}
	if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && UTSAndroid.getUniActivity()!.getApplicationInfo().targetSdkVersion >= 29) || (Build.VERSION.SDK_INT >= 30)) {
		try {
DCloud-yyl's avatar
DCloud-yyl 已提交
404
			var fis = getInputStream(fromFilePath)
405 406 407 408 409 410 411 412 413 414 415
			if (fis == null) {
				return false
			}
			var mimeType : string | null = null
			if (isVideo) {
				var retriever = new MediaMetadataRetriever()
				retriever.setDataSource(fromFilePath)
				mimeType = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE);
			} else {
				let opt = new BitmapFactory.Options();
				opt.inJustDecodeBounds = true;
DCloud-yyl's avatar
DCloud-yyl 已提交
416
				BitmapFactory.decodeStream(fis, null, opt)
417
				mimeType = opt.outMimeType;
DCloud-yyl's avatar
DCloud-yyl 已提交
418
				fis.close()
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
			}
			if (mimeType == null) {
				return false
			}
			var uri = MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL)
			if (mimeType!.startsWith("image/")) {
				uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
			} else if (mimeType!.startsWith("video/")) {
				uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
			} else if (mimeType!.startsWith("audio/")) {
				uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
			}
			var resolver = UTSAndroid.getUniActivity()!.getContentResolver()
			var contentValue = new ContentValues()
			let fileName = fromFilePath.substring(fromFilePath.lastIndexOf("/") + 1)
			contentValue.put(MediaStore.Files.FileColumns.DISPLAY_NAME, fileName)
			contentValue.put(MediaStore.Files.FileColumns.MIME_TYPE, mimeType!)
			contentValue.put(MediaStore.Files.FileColumns.TITLE, fileName)
			contentValue.put(MediaStore.Images.Media.RELATIVE_PATH, "DCIM/Camera/")
			let insertUri = resolver.insert(uri, contentValue)
			if (insertUri == null) {
				return false
			}
			let fos = resolver.openOutputStream(insertUri)
			if (fos == null) {
				return false
			}
DCloud-yyl's avatar
DCloud-yyl 已提交
446 447 448 449
			fis = getInputStream(fromFilePath)
			if(fis == null) {
				return false
			}
450
			var byteArrays = ByteArray(102400 * 2)
DCloud-yyl's avatar
DCloud-yyl 已提交
451
			var c = fis!.read(byteArrays)
452 453
			while (c > 0) {
				fos!.write(byteArrays, 0, c)
DCloud-yyl's avatar
DCloud-yyl 已提交
454
				c = fis!.read(byteArrays)
455 456
			}
			fos!.close()
DCloud-yyl's avatar
DCloud-yyl 已提交
457
			fis!.close()
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
			return true
		} catch (e : Exception) {
			e.printStackTrace();
			return false
		}
	} else {
		try {
			return copyFile(fromFilePath, toFilePath)
		} catch (e) {
			return false
		}
	}
}

function sendSaveToAlbumBroad(src : string) {
	let intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + src))
	getUniActivity()!.sendBroadcast(intent)
}

let DCIM_PATH = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + "/Camera/"
class SaveToAlbumTarget extends CustomTarget<File> {
	options : SaveImageToPhotosAlbumOptions
	saveToAlbum : boolean
	constructor(options : SaveImageToPhotosAlbumOptions, saveToAlbum : boolean) {
		super();
		this.options = options
		this.saveToAlbum = saveToAlbum
	}

	override onResourceReady(resource : File, transition : Transition<KotlinVarianceAnnotationIn<File>> | null) {
		let bitmapOption = new BitmapFactory.Options();
		bitmapOption.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(resource.getPath(), bitmapOption)
		let mimeType : string | null = bitmapOption.outMimeType
		if (!TextUtils.isEmpty(mimeType) && mimeType!.contains("/")) {
			mimeType = mimeType!.substring(mimeType!.indexOf("/") + 1)
		}

		// 拼接保存路径
		let path = DCIM_PATH + System.currentTimeMillis()
		if (!TextUtils.isEmpty(mimeType)) {
			path += "." + mimeType
		} else {
			path += ".jpg"
		}
		if (copyFileToPublicPath(resource.getPath(), path, false)) {
			if (this.saveToAlbum) {
				sendSaveToAlbumBroad(path)
			}
DCloud-yyl's avatar
DCloud-yyl 已提交
507
			let success : SaveImageToPhotosAlbumSuccess = {
508 509 510 511 512
				"path": path
			}
			this.options.success?.(success)
			this.options.complete?.(success)
		} else {
DCloud-yyl's avatar
DCloud-yyl 已提交
513
			let imageInfoCallback = new MediaErrorImpl(1101004, UniError_SaveImageToPhotosAlbum);
514 515 516 517 518 519 520 521 522
			this.options.fail?.(imageInfoCallback)
			this.options.complete?.(imageInfoCallback)
		}
	}

	override onLoadCleared(placeholder : Drawable | null) {
	}

	override onLoadFailed(errorDrawable : Drawable | null) {
DCloud-yyl's avatar
DCloud-yyl 已提交
523
		let imageInfoCallback = new MediaErrorImpl(1101004, UniError_SaveImageToPhotosAlbum);
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
		this.options.fail?.(imageInfoCallback)
		this.options.complete?.(imageInfoCallback)
	}
}

export const compressImage : CompressImage = function (options : CompressImageOptions) {
	transcodeImage(options)
}

export const chooseVideo : ChooseVideo = function (options : ChooseVideoOptions) {
	chooseMediaVideo(options)
}


export const getVideoInfo : GetVideoInfo = function (options : GetVideoInfoOptions) {
	let result = getVideoMetadata(options.src)
DCloud-yyl's avatar
DCloud-yyl 已提交
540 541 542 543
	if (result instanceof GetVideoInfoSuccess) {
		options.success?.(result as GetVideoInfoSuccess)
	} else if (result instanceof MediaErrorImpl)
		options.fail?.(result as MediaErrorImpl)
544 545 546 547 548
	options.complete?.(result)
}


export const saveVideoToPhotosAlbum : SaveVideoToPhotosAlbum = function (options : SaveVideoToPhotosAlbumOptions) {
DCloud-yyl's avatar
DCloud-yyl 已提交
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
		let requestPermissionList : Array<string> = [Manifest.permission.WRITE_EXTERNAL_STORAGE];
		UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, requestPermissionList, (a : boolean, b : string[]) => {
			loadFile(options);
		}, (a : boolean, b : string[]) => {
			let error = new MediaErrorImpl(1101005, UniError_SaveVideoToPhotosAlbum);
			options.fail?.(error);
			options.complete?.(error);
		})
	} else {
		loadFile(options);
	}
}

function loadFile(options : SaveVideoToPhotosAlbumOptions) {
564
	if (TextUtils.isEmpty(options.filePath)) {
DCloud-yyl's avatar
DCloud-yyl 已提交
565
		let error = new MediaErrorImpl(1101003, UniError_SaveVideoToPhotosAlbum);
566 567 568 569 570
		options.fail?.(error)
		options.complete?.(error)
		return
	}
	let originalPath = UTSAndroid.convert2AbsFullPath(options.filePath)
DCloud-yyl's avatar
DCloud-yyl 已提交
571 572 573
	if (originalPath.startsWith(ASSETS_PATH)) {
		originalPath = originalPath.replace(ASSETS_PATH, "")
	}
574
	if (!new File(originalPath).exists()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
575
		let error = new MediaErrorImpl(1101003, UniError_SaveVideoToPhotosAlbum);
576 577 578 579 580 581 582
		options.fail?.(error)
		options.complete?.(error)
		return
	}
	let path = DCIM_PATH + originalPath.substring(originalPath.lastIndexOf("/") + 1)
	if (copyFileToPublicPath(originalPath, path, true)) {
		sendSaveToAlbumBroad(path)
DCloud-yyl's avatar
DCloud-yyl 已提交
583
		let success : SaveVideoToPhotosAlbumSuccess = {
584 585 586 587 588
			// "path": path
		}
		options.success?.(success)
		options.complete?.(success)
	} else {
DCloud-yyl's avatar
DCloud-yyl 已提交
589
		let error = new MediaErrorImpl(1101006, UniError_SaveVideoToPhotosAlbum);
590 591 592 593 594 595 596 597
		options.fail?.(error)
		options.complete?.(error)
	}
}

export const compressVideo : CompressVideo = function (options : CompressVideoOptions) {
	transcodeVideo(options)
}