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_915918417 小时前
移动端 HTTPS 抓包实战,多工具组合分析与高效排查指南
数据库·网络协议·ios·小程序·https·uni-app·iphone
雪芽蓝域zzs11 小时前
uni-app 将 base64 图片编码转为 Blob 本地文件路径
网络协议·udp·uni-app
星光一影13 小时前
陪诊陪检系统源码,陪诊小程序,陪诊APP,陪诊服务,家政上门系统,居家护理陪护源码
mysql·小程序·uni-app·php
Qlittleboy14 小时前
uniapp里 rich-text 里的img图片如何控制最大宽度
uni-app
阿奇__14 小时前
uniapp h5 app 小程序获取当前定位
小程序·uni-app
*小雪14 小时前
uniapp写H5授权登录及分享,返回到目标页面
开发语言·javascript·uni-app
一只程序熊15 小时前
uniapp uniim ios配置消息推送
uni-app
2501_9151063217 小时前
iOS性能调优的系统化实践,从架构分层到多工具协同的全流程优化指南(开发者深度版)
android·ios·小程序·架构·uni-app·iphone·webview
TE-茶叶蛋1 天前
Uniapp运行MuMu模拟器
uni-app
一人一程温一壶酒1 天前
微信小程序uniapp开发附源码——图片加水印
微信小程序·uni-app·notepad++