梨子1111
梨子1111
  • 发布:2020-09-16 16:47
  • 更新:2024-05-30 16:06
  • 阅读:1522

uniapp能获取手机可用存储空间吗

分类:uni-app

如题,uniapp能获取手机可用存储空间吗

2020-09-16 16:47 负责人:无 分享
已邀请:
星桦呀

星桦呀

同问,大佬有解决方法了吗

DCloud_Android_DQQ

DCloud_Android_DQQ

暂无官方api,建议使用插件实现

未设置昵称

未设置昵称

/**

  • @deprecated 获取手机剩余空间
  • @returns {string}
    */

export default function() {
var internalMemSize = {};
if (plus.os.name == "Android") {
var environment = plus.android.importClass("android.os.Environment");
var statFs = plus.android.importClass("android.os.StatFs");
var files = plus.android.importClass("java.io.File");
var Files = environment.getDataDirectory();
var StatFs = new statFs(Files.getPath());
var blockSize = parseFloat(StatFs.getBlockSize());
var blockCount = parseFloat(StatFs.getBlockCount());
internalMemSize = blockSize * blockCount;
} else if (plus.os.name == "iOS") {
var BundleClass = plus.ios.importClass("NSBundle");
var BundleObj = BundleClass.mainBundle();
var filenamagerobj = plus.ios.newObject("NSFileManager");
var FileAttr = plus.ios.invoke(filenamagerobj, "attributesOfFileSystemForPath:error:", BundleObj.bundlePath(),null);
var surplus = plus.ios.invoke(FileAttr, "objectForKey:", "NSFileSystemFreeSize");
var NSFileSystemFreeSize = plus.ios.newObject("NSNumberFormatter");
internalMemSize = plus.ios.invoke(NSFileSystemFreeSize, "stringFromNumber:", surplus);
}
return internalMemSize
}

FullStack

FullStack - 【插件开发】【专治疑难杂症】【ios上架、马甲包、白包、过审、已成功上架过几百个】【多款插件已上架:https://ext.dcloud.net.cn/publisher?id=22130】【非诚勿扰】QQ:543610866

s***@163.com

s***@163.com

onLaunch: function () {
var internalMemSize = {};
var internalMemSizeGB = 0;
var internalMemSizeMB = 0;
var usedMemSizeGB = 0;
var usedMemSizeMB = 0;
if (plus.os.name == "Android") {
var environment = plus.android.importClass("android.os.Environment");
var statFs = plus.android.importClass("android.os.StatFs");
var files = plus.android.importClass("java.io.File");
var Files = environment.getDataDirectory();
var StatFs = new statFs(Files.getPath());
var blockSize = parseFloat(StatFs.getBlockSize());
var blockCount = parseFloat(StatFs.getBlockCount());
var availableBlocks = parseFloat(StatFs.getAvailableBlocks());
internalMemSize = blockSize blockCount;
var availableMemSize = blockSize
availableBlocks;
var usedMemSize = internalMemSize - availableMemSize;

} else if (plus.os.name == "iOS") {  
  var BundleClass = plus.ios.importClass("NSBundle");  
  var BundleObj = BundleClass.mainBundle();  
  var filenamagerobj = plus.ios.newObject("NSFileManager");  
  var FileAttr = plus.ios.invoke(filenamagerobj, "attributesOfFileSystemForPath:error:", BundleObj.bundlePath(), null);  
  var totalCapacity = plus.ios.invoke(FileAttr, "objectForKey:", "NSFileSystemSize");  
  var availableCapacity = plus.ios.invoke(FileAttr, "objectForKey:", "NSFileSystemFreeSize");  

  var NSFileSystemFormatter = plus.ios.newObject("NSNumberFormatter");  
  internalMemSize = plus.ios.invoke(NSFileSystemFormatter, "stringFromNumber:", totalCapacity);  
  var availableMemSize = plus.ios.invoke(NSFileSystemFormatter, "stringFromNumber:", availableCapacity);  
  var usedMemSize = totalCapacity - availableCapacity;  
}  

if (internalMemSize) {  
  internalMemSizeGB = (internalMemSize / (1024 * 1024 * 1024)).toFixed(2);  
  internalMemSizeMB = (internalMemSize / (1024 * 1024)).toFixed(2);  
  usedMemSizeGB = (usedMemSize / (1024 * 1024 * 1024)).toFixed(2);  
  usedMemSizeMB = (usedMemSize / (1024 * 1024)).toFixed(2);  
}  

console.log('Total Internal Memory Size: ', internalMemSizeGB, 'GB (', internalMemSizeMB, 'MB)');  
console.log('Used Internal Memory Size: ', usedMemSizeGB, 'GB (', usedMemSizeMB, 'MB)');  

},在App.vue中

要回复问题请先登录注册