儲存裝置資訊

由 3 位貢獻者:

此文件需要技術審查。

此文件需要編輯審查。

儲存裝置資訊 (Device Storage)

DeviceStorage API 提供 SD 記憶卡內的資料存取介面,可讀取圖片檔、聲音檔、影片檔,與 SD 記憶卡的其他檔案,並檢視 SD 記憶卡空間的使用率。
由於牽涉到使用者的儲存裝置資訊,目前僅開放 Privileged/Certified App 能夠使用此 API。你的 App 必須提出「使用此 API」的聲明,並且通過審查方可使用。

權限宣告:依照檔案類型 (圖片、影片、聲音、其他) 而有不同宣告方式,這種宣告另分為「readonly」、「readwrite」、「readcreate」共 3 種級別,分別代表「唯讀」、「讀取與修改」、「讀取與創建」。你可依照 App 需求作適當的權限宣告。

"permissions": {
  "device-storage:pictures": { "access": "readonly" },
  "device-storage:music": { "access": "readcreate" },
  "device-storage:videos": { "access": "readwrite" },
  "device-storage:sdcard": { "access": "readonly" }
}

1. 音樂檔案列表

  • 權限宣告:"device-storage:music": { "access": "readonly" }
    var type = 'music';
    var deviceStorage = navigator.getDeviceStorage(type);
    if (!deviceStorage) {
      console.warn('Error: cannot get DeviceStorage for ' + type);
      return;
    }  
    var request = deviceStorage.enumerate();
    request.onsuccess = function(e) {
      var file = request.result;
      console.log('file name ' + file.name);
      console.log('file size ' + file.size);
      console.log('file type ' + file.type);
      console.log('file last Modified Date ' + file.lastModifiedDate);
      request.continue();                                                                                                                                                               
    };
    request.onerror = function() {
      console.warn('Error: cannot list files in SD card - ' + request.error.name);
    };

2. 讀取某個 SD 卡檔案

  • 權限宣告:"device-storage:pictures": { "access": "readonly" }
    var type = 'pictures';
    var deviceStorage = navigator.getDeviceStorage(type);
    if (!deviceStorage) {
      console.warn('Error: cannot get DeviceStorage for ' + type);
      return;
    }
    var filename = 'downloads/DSC02798.JPG';
    var request = deviceStorage.get(filename);
    request.onsuccess = function(e) {
      var file = request.result;
      var url = URL.createObjectURL(file);
      var myImage = document.getElementById('profilePhoto');
      myImage.src = url;                                                                                                                                                                    
    };
    request.onerror = function() {
      console.warn('Error: get file from SD card - ' + request.error.name);
    };

3. 寫入檔案到 SD 卡

  • 權限宣告:"device-storage:sdcard": { "access": "readcreate" }
    var type = 'sdcard';
    var deviceStorage = navigator.getDeviceStorage(type);
    if (!deviceStorage) {
      console.warn('Error: cannot get DeviceStorage for ' + type);
      return;
    }
    var filename = 'test-' + Math.random().toString() + '.txt';
    var pathname = 'test-tmp/' + filename;
    console.log('file path: ' + pathname);
    var blob = new Blob(['hello', 'world'], {type: 'text/plain'});
    var request = deviceStorage.addNamed(blob, pathname);
    request.onsuccess = function(e) {
     // try to get the file
      var readRequest = deviceStorage.get(pathname);
      readRequest.onsuccess = function() {
        var file = readRequest.result;
        var reader = new FileReader();
        reader.onload = function(e) {
          console.log('file content ' + e.target.result);
        }; 
        reader.readAsText(file);
      }; 
      readRequest.onerror = function() {
        console.warn("Error: read file failure - " + request.error.name);
      }; 
    }; 
    request.onerror = function() {
      console.warn('Error: write file failure - ' + request.error.name);
    };

4. SD 卡空間使用率

  • 權限宣告:"device-storage:sdcard": { "access": "readonly" }
    var type = 'sdcard';
    var deviceStorage = navigator.getDeviceStorage(type);
    if (!deviceStorage) {
      console.warn('Error: cannot get DeviceStorage for ' + type);
      return;
    }    
    var request = deviceStorage.stat();
    request.onsuccess = function(e) {
      var totalSize = e.target.result.totalBytes;
      var freeBytes = e.target.result.freeBytes;
      console.log('totalSize ' + totalSize + ', freeBytes ' + freeBytes);
    }; 
    request.onerror = function() {
      console.warn('Error: cannot get stat of SD card - ' + request.error.name);
    };

 

Document Tags and Contributors

Contributors to this page: MashKao, teoli, evelynhung
最近更新: MashKao,
隱藏側邊欄