HarmonyOS 鸿蒙Next下载视频不走process回调且大小为0B

发布于 1周前 作者 sinazl 最后一次编辑是 5天前 来自 鸿蒙OS

HarmonyOS 鸿蒙Next下载视频不走process回调且大小为0B <markdown _ngcontent-ubn-c149="" class="markdownPreContainer">

import request from @ohos.request”;
import common from @ohos.app.ability.common”;
import HashMap from @ohos.util.HashMap”;

export function down(fileUrl: string, context: common.UIAbilityContext){
const fileSuffixName = fileUrl.split(’.’)[fileUrl.split(’.’).length - 1]
const fileFullName = fileUrl.split(’/’)[fileUrl.split(’/’).length - 1]
console.log(‘context.filesDir’, context.filesDir)
const filePath = <span class="hljs-cbracket">${context.filesDir}</span>/<span class="hljs-cbracket">${ fileFullName}</span>;
console.error(filePath);
let header:HashMap<string,string> = new HashMap<string,string>()
header.set(‘Content-Type’,‘video/mp4’)
try {
request.downloadFile(context, {
url: fileUrl,
enableMetered: true,
enableRoaming: true,
filePath: filePath,
header:header}, (err, data) => {
if (err) {
console.error('Failed to request the download. Cause: ’ + JSON.stringify(err));
return;
}
// 监听文件下载的实时情况 receivedSize已下载 totalSize总大小
data.on(‘progress’, (receivedSize, totalSize) => {
console.info(“upload totalSize:” + totalSize + "  receivedSize:" + receivedSize);
})
// 监听文件下载完成
data.on(‘complete’, () => {
console.info(‘Download task completed.’);
})
});
} catch (err) {
console.error('err.code :  + err.code + ', err.message : ’ + err.message);
}
}

我用来测试的视频和图片的地址是:

down('https://www.w3school.com.cn/i/video/shanghai.mp4',context)
down('http://e.hiphotos.baidu.com/image/pic/item/a1ec08fa513d2697e542494057fbb2fb4316d81e.jpg',context)

更多关于HarmonyOS 鸿蒙Next下载视频不走process回调且大小为0B的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复
import request from "@ohos.request";
import common from "@ohos.app.ability.common";
import HashMap from "@ohos.util.HashMap";
export function down(fileUrl: string) {
let context = getContext()
const fileSuffixName = fileUrl.split(’.’)[fileUrl.split(’.’).length - 1]
const fileFullName = fileUrl.split(’/’)[fileUrl.split(’/’).length - 1]
console.log(‘zcontext.filesDir’, context.filesDir)
// const filePath = ${context.filesDir}/${fileFullName};
const filePath = context.filesDir + ‘/’ + new Date().toLocaleTimeString() + ‘.mp4
console.error(‘zcontext.filePath’, filePath);
let header: HashMap<string, string> = new HashMap<string, string>()
header.set(‘Content-Type’, video/mp4’)
// header.set(‘Content-Type’, ‘image/jpg’)
try {
request.downloadFile(context, {
url: fileUrl,
enableMetered: true,
enableRoaming: true,
filePath: filePath,
header: header
}, (err, data) => {
if (err) {
console.error('z–Failed to request the download. Cause: ’ + JSON.stringify(err));
return;
}
// 监听文件下载的实时情况 receivedSize已下载 totalSize总大小
data.on(‘progress’, (receivedSize, totalSize) => {
console.info(“z–upload totalSize:” + totalSize + "  receivedSize:" + receivedSize);
})
// 监听文件下载完成
data.on(‘complete’, () => {
console.info(‘z–Download task completed.’);
})
});
} catch (err) {
console.error('zerr.code :  + err.code + ', err.message : ’ + err.message);
}
}


down(‘https://www.w3school.com.cn/i/video/shanghai.mp4’)

image.png

更多关于HarmonyOS 鸿蒙Next下载视频不走process回调且大小为0B的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


找HarmonyOS工作还需要会Flutter的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:https://www.bilibili.com/video/BV1S4411E7LY/?p=17

其实我要做的需求是将视频保存到用户相册,接下来是把下载好的视频移动到相册是吗?

针对您提到的HarmonyOS鸿蒙Next系统中下载视频不走process回调且文件大小为0B的问题,这通常可能由以下几个原因引起:

  1. 下载任务未正确初始化:检查下载任务的初始化代码,确保所有必要的参数(如URL、保存路径等)都已正确设置。

  2. 权限问题:确认应用已获取存储权限,特别是针对外部存储的读写权限。在HarmonyOS中,权限管理可能有所不同,请查阅最新的权限管理文档。

  3. 网络问题:下载失败可能是由于网络连接不稳定或服务器问题。尝试在不同的网络环境下进行下载,或检查服务器状态。

  4. 系统Bug:由于您提到的是鸿蒙Next系统,可能存在系统级别的Bug。关注华为官方发布的更新和补丁,看是否有相关的修复。

  5. 回调实现问题:检查process回调的实现,确保回调方法没有被错误地覆盖或未正确注册。

鸿蒙Next教程已发布,可以先学学https://www.itying.com/category-93-b0.html

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!