mozilla

Revision 349911 of 儲存裝置資訊

  • Revision slug: Mozilla/Firefox_OS/第三方應用開發者指南/儲存裝置資訊
  • Revision title: 儲存裝置資訊
  • Revision id: 349911
  • Created:
  • Creator: evelynhung
  • Is current revision? No
  • Comment

Revision Content

儲存裝置資訊 (Device Storage)

DeviceStorage API 提供 SD 卡內資料存取介面,可以讀取圖片檔、聲音檔、影片檔,以及 SD 卡的其他檔案,以及檢視 SD 卡空間使用率。
由於它牽涉到使用者的儲存裝置資訊,目前僅開放審查通過的應用(privileged/certified)能夠使用這個 API。您的應用必須提出使用這個 API 的聲明,並且通過審查方可使用。

權限宣告:依照檔案類型(圖片、影片、聲音、其他)有不同宣告方式,每種宣告也有 "readonly", "readwrite", "readcreate" 三種級別,分別代表「只讀取」、「讀取與修改」、「讀取與創建」。您可依照應用需求作適當的權限宣告。

"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);
    };

 

Revision Source

<h3 id=".E5.84.B2.E5.AD.98.E8.A3.9D.E7.BD.AE.E8.B3.87.E8.A8.8A_(Device_Storage)">儲存裝置資訊 (Device Storage)</h3>
<p>DeviceStorage API 提供 SD 卡內資料存取介面,可以讀取圖片檔、聲音檔、影片檔,以及 SD 卡的其他檔案,以及檢視 SD 卡空間使用率。<br />
  由於它牽涉到使用者的儲存裝置資訊,目前僅開放審查通過的應用(privileged/certified)能夠使用這個 API。您的應用必須提出使用這個 API 的聲明,並且通過審查方可使用。<br />
  <br />
  權限宣告:依照檔案類型(圖片、影片、聲音、其他)有不同宣告方式,每種宣告也有 "readonly", "readwrite", "readcreate" 三種級別,分別代表「只讀取」、「讀取與修改」、「讀取與創建」。您可依照應用需求作適當的權限宣告。</p>
<pre>
"permissions": {
  "device-storage:pictures": { "access": "readonly" },
  "device-storage:music": { "access": "readcreate" },
  "device-storage:videos": { "access": "readwrite" },
  "device-storage:sdcard": { "access": "readonly" }
}</pre>
<h4 id="1._.E9.9F.B3.E6.A8.82.E6.AA.94.E6.A1.88.E5.88.97.E8.A1.A8_(.22device-storage.3Amusic.22.3A.7B_.22access.22.3A_.22readonly.22_.7D)">1. 音樂檔案列表</h4>
<ul>
  <li>權限宣告:"device-storage:music": { "access": "readonly" }</li>
</ul>
<pre>
    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);
    };</pre>
<h4 id="2._.E8.AE.80.E5.8F.96.E6.9F.90.E5.80.8B_SD_.E5.8D.A1.E6.AA.94.E6.A1.88_(.22device-storage.3Apictures.22.3A.7B_.22access.22.3A_.22readonly.22_.7D)">2. 讀取某個 SD 卡檔案</h4>
<ul>
  <li>權限宣告:"device-storage:pictures": { "access": "readonly" }</li>
</ul>
<pre>
    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);
    };</pre>
<h4 id="3._.E5.AF.AB.E5.85.A5.E6.AA.94.E6.A1.88.E5.88.B0_SD_.E5.8D.A1_(.22device-storage.3Asdcard.22.3A.7B_.22access.22.3A_.22readcreate.22_.7D)">3. 寫入檔案到 SD 卡</h4>
<ul>
  <li>權限宣告:"device-storage:sdcard": { "access": "readcreate" }</li>
</ul>
<pre>
    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);
    };</pre>
<h4 id="4._SD_.E5.8D.A1.E7.A9.BA.E9.96.93.E4.BD.BF.E7.94.A8.E7.8E.87_(.22device-storage.3Asdcard.22.3A.7B_.22access.22.3A_.22readonly.22_.7D)">4. SD 卡空間使用率</h4>
<ul>
  <li>權限宣告:"device-storage:sdcard": { "access": "readonly" }</li>
</ul>
<pre>
    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);
    };</pre>
<p>&nbsp;</p>
Revert to this revision