uniapp内把视频mp4的base64保持到手机文件系统

我们用了uniapp中的webview实现调用摄像头视频流并显示界面,webview引用的界面地址是同一个项目中的html界面,这个界面放了video,显示视频,vue界面中引用此webview。webview和vue界面通讯传参也成了问题,在录制视频保存时,也是遇到了很大挑战。这篇博客只记录了视频base64保存安卓手机的代码。

javascript 复制代码
//调用示例
var fileFullName = '/storage/emulated/0/video_' + that.getFormattedDate() + '.mp4';
that.base64ToFile(base64String,fileFullName);


//去掉头部标识后的,base64字符串转byteArray
function base64ToByteArray(base64Str) {
		console.log(base64Str.length)
		const binaryString = atob(base64Str);
		const uint8Array = new Uint8Array(binaryString.length);

		for (let i = 0; i < binaryString.length; i++) {
			uint8Array[i] = binaryString.charCodeAt(i);
		}
		let arr = []
		Array.from(uint8Array).map(num => {
			arr.push(num >= 128 ? (num - 256) : num)
		})
		return arr;
},
//获取日期的年月日时分秒
function getFormattedDate() {
    const now = new Date();
    const year = now.getFullYear();        // 获取年份
    const month = String(now.getMonth() + 1).padStart(2, '0');  // 获取月份,注意月份是从0开始的,因此需要加1,且格式化为两位数
    const day = String(now.getDate()).padStart(2, '0');         // 获取日期,格式化为两位数
    const hours = String(now.getHours()).padStart(2, '0');      // 获取小时,格式化为两位数
    const minutes = String(now.getMinutes()).padStart(2, '0');  // 获取分钟,格式化为两位数
    const seconds = String(now.getSeconds()).padStart(2, '0');  // 获取秒数,格式化为两位数

    // 拼接为所需格式:YYYYMMDDHHmmss
    return `${year}${month}${day}${hours}${minutes}${seconds}`;
},
/**  
* base64字符串转成文件保存手机文件系统
 * @param {String} base64Str // 允许包含前缀,可以是图片、视频 base64
 * @param {String} fileName // 文件名称:video.mp4、a.jpeg
 */ 
function base64ToLocalFile (base64Str, fullPath) {
		var that = this;
		// 去除base64前缀
		var index=base64Str.indexOf(',')  
		var base64Str=base64Str.slice(index+1,base64Str.length)
		console.log(base64Str.length)
		
		plus.io.requestFileSystem(plus.io.PRIVATE_DOC,function(fs){  
			console.log(fullPath)
			
			let platform = uni.getSystemInfoSync().platform
			if(platform == 'android'){    
				
				var FileOutputStream = plus.android.importClass("java.io.FileOutputStream");  
				try{
					var out = new FileOutputStream(fullPath);  
					let bytes = that.base64ToByteArray(base64Str);
					console.log(bytes.length)
					console.log(fullPath)
					out.write(bytes);   
					out.close();
					console.log('写手机文件系统完成')
				}catch(e){  
					console.log(e.message);  
				}
			}else if(platform == 'ios'){  
				var NSData = plus.ios.importClass('NSData');  
				var nsData = new NSData();  
				nsData = nsData.initWithBase64EncodedStringoptions(base64Str,0);  
				if (nsData) {  
					nsData.plusCallMethod({writeToFile: fullPath,atomically:true});  
					plus.ios.deleteObject(nsData);  
				}  
				
			}
		})
	}

参考:

原博主:uniapp中图片视频的base64保存手机上

原博主:base64遇到大文件处理方法

相关推荐
2501_9159090617 小时前
tcpdump 抓包数据分析实战,命令、过滤、常见故障定位与真机补充流程
网络·测试工具·ios·小程序·uni-app·iphone·tcpdump
赵庆明老师1 天前
Uniapp微信小程序开发:微信小程序支付功能后台代码
微信小程序·小程序·uni-app
曹申阳1 天前
1. 使用VSCode开发uni-app环境搭建
ide·vscode·uni-app
雪芽蓝域zzs1 天前
uniapp开发 APP嵌入另一个APP打包的wgt文件,实现点击携带参数跳转到wgtAPP的某一个页面
uni-app·apache
00后程序员张1 天前
tcpdump 抓包分析,命令、过滤技巧、常见症状定位与移动真机补充方案
网络·测试工具·ios·小程序·uni-app·iphone·tcpdump
BumBle1 天前
基于UniApp实现DeepSeek AI对话:流式数据传输与实时交互技术解析
前端·uni-app
会点法律的程序员1 天前
小程序 地理位置授权怎么搞
前端·小程序·uni-app
重生之我是菜鸡程序员1 天前
uniapp 顶部通知 上滑隐藏
前端·javascript·uni-app
FliPPeDround1 天前
告别 uni-app 启动烦恼:@uni-helper/unh 让开发流程更顺畅
前端·微信小程序·uni-app
2501_915921431 天前
iOS 26 电耗监测与优化,耗电问题实战 + 多工具 辅助策略
android·macos·ios·小程序·uni-app·cocoa·iphone