Home > Web Front-end > uni-app > How to check how much free space is left on the phone in uniapp

How to check how much free space is left on the phone in uniapp

PHPz
Release: 2023-04-18 09:52:05
Original
1272 people have browsed it

As the storage space of mobile phones continues to expand, we like to store a large number of photos, videos, music, etc. However, when there is insufficient space on the mobile phone, various strange problems will occur, such as the mobile phone running slowly, the APP cannot be updated, and the system Crash etc. Therefore, it is particularly important to understand the usage of mobile phone storage space. This article will introduce how to check how much space is left on your phone through Uniapp, so that you can release storage space in time and avoid unnecessary trouble.

1. Introduction to Uniapp

Uniapp is a cross-platform development framework based on Vue.js and mini program technology. It can convert Vue.js code into iOS, Android, H5, mini program Programs and other multi-platform applications allow developers to publish applications across platforms with just one coding. Uniapp has many advantages such as high operating efficiency, high development efficiency, and strong cross-platform capabilities, making it the mainstream development tool chosen by many developers.

2. How to check the phone storage space

  1. Go to the phone settings page

First, we need to open the phone’s settings page and find the About phone option. After clicking, you can see the current storage space usage of your phone.

  1. Use Uniapp's FileSystemManager interface

Another method is to use Uniapp's FileSystemManager interface to check the usage of mobile phone storage space. FileSystemManager provides a set of asynchronous API interfaces that can access files and directories in the local file system, including the following methods:

a. getFileSystemManager()

This method can get the FileSystemManager Instance, calling other FileSystemManager methods requires obtaining the instance through this method and then operating.

b. stat(Object object)

This method is used to query related information of a file or directory, including creation time, modification time, size and other information. The incoming parameter object must contain the path attribute, which represents the path of the file or directory, as shown below:

uni.getFileSystemManager().stat({
  path: '/images',
  success: function (res) {
    console.log(res.size);
  }
});
Copy after login

c. getAvailableStorage(Object object)

This method is used to query the available storage space size. The incoming parameter object must contain the storageType attribute, indicating the type of storage space, as shown below:

uni.getFileSystemManager().getAvailableStorage({
  storage: 'internal', // 可选值internal, external
  success: function (res) {
    console.log(res.availableSize);
  }
});
Copy after login

Among them, the optional values ​​​​of storageType include internal and external, which represent the phone's built-in storage and external SD card respectively. storage device. After the call is successful, the size of the available storage space can be obtained through res.availableSize.

  1. Combining Uniapp with the native API

Finally, we can get more detailed storage space usage information by combining Uniapp with the native API. For example, in the Android system, we can call the Environment.getExternalStorageDirectory() method to obtain the path of the external SD card, and then obtain the available space size through the getUsableSpace() method of the File class, as shown below:

// Android平台外置SD卡的可用存储空间
if (uni.getSystemInfoSync().platform == 'android') {
  var sdCardPath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
  var sdCard = new File(sdCardPath);
  console.log(sdCard.getUsableSpace());
}
Copy after login

Similarly, in the iOS system, we can obtain the path of the sandbox Document directory through the NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) method, and then call the attributesOfFileSystemForPath:error: method of the NSFileManager class to obtain its file system related information, as shown below :

// iOS平台沙盒Document目录的存储空间信息
if (uni.getSystemInfoSync().platform == 'ios') {
  var documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
  var fileManager = NSFileManager.defaultManager();
  var attributes = fileManager.attributesOfFileSystemForPathError(documentPath, NULL);
  var availableSize = attributes.objectForKey(NSFileSystemFreeSize);
  console.log(availableSize);
}
Copy after login

3. Summary

This article introduces three methods to check the usage of mobile phone storage space through Uniapp: go to the mobile phone settings page, use the FileSystemManager interface, and use Uniapp in combination with the native API. . Compared with the former, the latter two methods can not only check the storage space size, but also obtain more detailed storage space usage information. During use, it should be noted that the API usage methods of different systems may be different, and corresponding adjustments need to be made according to the specific platform. Through the introduction of this article, I believe you have understood how to use Uniapp to check the usage of mobile phone storage space. I hope it will be helpful to you.

The above is the detailed content of How to check how much free space is left on the phone in uniapp. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template