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遇到大文件处理方法

相关推荐
软工的小白2 小时前
uniapp开发前端静态视频界面+如何将本地视频转换成网络地址
uni-app·音视频
2501_916008894 小时前
iOS 抓包工具有哪些?全面盘点主流工具与功能对比分析
android·ios·小程序·https·uni-app·iphone·webview
2501_915921434 小时前
iOS混淆工具实战 视频流媒体类 App 的版权与播放安全保护
android·ios·小程序·https·uni-app·iphone·webview
NewChapter °5 小时前
如何通过 Gitee API 上传文件到指定仓库
前端·vue.js·gitee·uni-app
SY_FC7 小时前
uniapp阿里云验证码使用
阿里云·uni-app·notepad++
—Qeyser8 小时前
好看的背景颜色 uniapp+小程序
小程序·uni-app·uniapp·微信小游戏
2501_916008898 小时前
uni-app iOS 日志与崩溃分析全流程 多工具协作的实战指南
android·ios·小程序·https·uni-app·iphone·webview
2501_915921439 小时前
iOS混淆工具实战 在线教育直播类 App 的课程与互动安全防护
android·安全·ios·小程序·uni-app·iphone·webview
织_网11 小时前
UniApp 页面通讯方案全解析:从 API 到状态管理的最佳实践
前端·javascript·uni-app
yuehua_zhang13 小时前
uni app 的app端 写入运行日志到指定文件夹。
前端·javascript·uni-app