鸿蒙
获取版本号
bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION)
.then((bundleInfo) => {
this.appNum = bundleInfo.versionName;
//bundleInfo.versionCode
}).catch((err: BusinessError) => {
Logger.debug('AboutAppPage', err.message)
})
上传单张图片
async postSingleImage<T>(ctx: Context,T: object, filePath:string, apiUrl: string, completeBack: (data: T | AppError) => void
) {
let file: fs.File = await fs.openSync(filePath, fs.OpenMode.READ_ONLY)
let pathDir = ctx.getApplicationContext().cacheDir
let destName = ${new Date().getTime()}.jpeg
let destPath = ${pathDir}/${destName}
fs.copyFileSync(file?.fd, destPath)
if (file) {
fs.closeSync(file.fd);
}
let formData = new FormData()
formData.append('file', destPath, { filename: destName, type: "image/jpeg"})
axios.post<string, AxiosResponse<T>, FormData>(
this.replaceBaseUrl(apiUrl),
formData,
{
headers: { 'Content-Type': 'multipart/form-data' },
context: getContext(this),
onUploadProgress: (progressEvent: AxiosProgressEvent): void => {
console.error(
progressEvent && progressEvent.loaded && progressEvent.total
? Math.ceil((progressEvent.loaded / progressEvent.total) * 100) + '%'
: '0%'
);
},
}
)
.then((res: AxiosResponse<T>) => {
completeBack(res.data);
console.info("completeBack: " + JSON.stringify(res.data));
})
.catch((err: AxiosError) => {
completeBack(err as AppError);
console.error("completeBack: " + JSON.stringify(err));
});
}
fun getImageInfo(
path: String
): MultipartBody.Part {
val temp = File(path)
val bitmap = BitmapFactory.decodeFile(path)
val img = ImageUtils.compressByQuality(bitmap, 1048576L, true)
val file = File(AppCore.context().getExternalFilesDir(null).toString(), temp.name)
FileIOUtils.writeFileFromBytesByChannel(file, img, true)
val requestBody: RequestBody = file.asRequestBody("image/png".toMediaTypeOrNull())
val partBody = MultipartBody.Part.createFormData("file", file.name, requestBody)
return partBody
}
public static void initImages(Object url, ImageView imageView) {
try {
SoftReference<ImageView> reference = new SoftReference<>(imageView);
RequestOptions options = RequestOptions.placeholderOf(0)
.error(0);
Glide.with(reference.get())
.load(url)
.apply(options)
.into(imageView);
} catch (Exception e) {
e.printStackTrace();
}
}