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

相关推荐
小徐_233314 小时前
uni-app vue3 也能使用 Echarts?Wot Starter 是这样做的!
前端·uni-app·echarts
iOS阿玮18 小时前
永远不要站在用户的对立面,挑战大众的公知。
uni-app·app·apple
xw520 小时前
uni-app中v-if使用”异常”
前端·uni-app
!win !20 小时前
uni-app中v-if使用”异常”
前端·uni-app
2501_915918411 天前
iOS 上架全流程指南 iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传 ipa 与审核实战经验分享
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张1 天前
iOS App 混淆与加固对比 源码混淆与ipa文件混淆的区别、iOS代码保护与应用安全场景最佳实践
android·安全·ios·小程序·uni-app·iphone·webview
00后程序员张1 天前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
海绵宝宝不喜欢侬2 天前
uniapp-微信小程序分享功能-onShareAppMessage
微信小程序·小程序·uni-app
2501_915106322 天前
Xcode 上传 ipa 全流程详解 App Store 上架流程、uni-app 生成 ipa 文件上传与审核指南
android·macos·ios·小程序·uni-app·iphone·xcode
xkxnq2 天前
Uniapp崩溃监控体系构建:内存泄漏三维定位法(堆栈/资源/线程)
uni-app