33 lines
1.1 KiB
Plaintext
33 lines
1.1 KiB
Plaintext
import Context from "android.content.Context"
|
|
import Bitmap from "android.graphics.Bitmap"
|
|
import BitmapFactory from "android.graphics.BitmapFactory"
|
|
import Base64 from "android.util.Base64"
|
|
import File from "java.io.File"
|
|
import FileOutputStream from "java.io.FileOutputStream"
|
|
|
|
export function toPngFile(data : string) : Promise<string> {
|
|
let fileName = Math.random().toString(16).substring(2, 16)
|
|
let baesestrAr = data.split(',') as string[]
|
|
let base64Data = baesestrAr[1]
|
|
let fileUrl = ''
|
|
|
|
try {
|
|
let context = UTSAndroid.getAppContext()!
|
|
let filepath = context.getExternalCacheDir()?.getPath() ?? ""
|
|
// 解码Base64字符串
|
|
let decodedBytes = Base64.decode(base64Data, Base64.DEFAULT)
|
|
let bitmap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.size)
|
|
let file = new File(filepath, fileName + '.png');
|
|
|
|
let fos = new FileOutputStream(file)
|
|
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
|
|
fileUrl = file.getPath();
|
|
|
|
} catch (error) {
|
|
console.error('处理图片错误:', error)
|
|
//TODO handle the exception
|
|
return Promise.reject(fileUrl) as Promise<string>
|
|
}
|
|
|
|
return Promise.resolve(fileUrl)
|
|
} |