2025-03-04

鸿蒙
获取版本号
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();
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容