2017-11-06 16 views
0

私のCordovaアプリでファイルの読み取り権限の問題が発生していますが、私の人生は正しく追跡できません。 Android 5とAndroid 6の間では、同じ呼び出しで全く異なる結果が表示されています(fileEntry)。 Androidでファイル/ディレクトリを読み取るために必要な変更があるかどうかを知っていますか> 5ファイルの検索結果がAndroid 6に返されません

Goryの詳細:私は(cordova create helloを呼び出す)だけcordova-plugin-fileでのHelloWorldアプリに出たコードをコピーして、同じ結果を取得しています。次のように私の変更点は次のとおりです。

私はindex.htmlの身体への単一の要素を追加しました:私もちょうどデバイスとリターンのさまざまなディレクトリを反復処理するために、index.jsに株式receivedEvent方法を変更し

 <div id="contents"></div> 

を何cordova-plugin-fileは私に与える:

receivedEvent: function (id) { 
    "use strict"; 

    var parentElement = document.getElementById(id), 
     listeningElement = parentElement.querySelector('.listening'), 
     receivedElement = parentElement.querySelector('.received'), 
     localURLs = [ 
      cordova.file.documentsDirectory, 
      cordova.file.externalRootDirectory, 
      cordova.file.sharedDirectory, 
      cordova.file.syncedDataDirectory 
     ], 
     DirsRemaining = localURLs.length, 
     i, 
     contents = "", 
     addFileEntry = function (entry) { 
      var dirReader = entry.createReader(); 
      dirReader.readEntries(
       function (entries) { 
        var i; 
        for (i = 0; i < entries.length; i += 1) { 
         if (entries[i].isDirectory === true) { 
          contents += "<h2> Directory: " + entries[i].fullPath + "</h2>"; 
          // Recursive -- call back into this subdirectory 
          DirsRemaining += 1; 
          addFileEntry(entries[i]); 
         } else { 
          contents += "<p> File: " + entries[i].fullPath + "</p>"; 
         } 
        } 
        DirsRemaining -= 1; 
        if (DirsRemaining <= 0) { 
         if (contents.length > 0) { 
          document.getElementById("contents").innerHTML = contents; 
         } else { 
          // nothing to select -- inform the user 
          document.getElementById("contents").innerHTML = "No documents found"; 
         } 
        } 
       }, 
       function (error) { 
        console.log("readEntries error: " + error.code); 
        contents += "<p>readEntries error: " + error.code + "</p>"; 
       } 
      ); 
     }, 
     addError = function (error) { 
      // log the error and continue processing 
      console.log("getDirectory error: " + error.code); 
      DirsRemaining -= 1; 
     }; 
    for (i = 0; i < localURLs.length; i += 1) { 
     if (localURLs[i] === null || localURLs[i].length === 0) { 
      DirsRemaining -= 1; 
      continue; // skip blank/non-existent paths for this platform 
     } 
     window.resolveLocalFileSystemURL(localURLs[i], addFileEntry, addError); 
    } 

    listeningElement.setAttribute('style', 'display:none;'); 
    receivedElement.setAttribute('style', 'display:block;'); 

    console.log('Received Event: ' + id); 
} 

可能なセキュリティ設定を:

私のplatforms/android/AndroidManifest.xmlファイルは、適切な権限が含まれています

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

は、ここで(それだけで株式一つだ)index.htmlの中で私のCSPです:ここで

<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;"> 

結果は以下のとおりです。

Android 5

のアンドロイド5.注ディレクトリおよびIが表示されなければならないcordova.file.externalRootDirectory下のサブディレクトリにファイルをいくつか持っているファイル(他の人が画面外です)

Android 6

アンドロイド6.注意。サブディレクトリ自体も表示する必要があります。ヒントの@greenappsへ

+1

を、あなた以上が要求された許可を確認するために、あなたのアプリのユーザーに確認するためのコードを追加する必要があります。 Googleの実行時アクセス許可。 – greenapps

答えて

0

Hatの先端が - この動作はコルドバ6.

アンドロイドで導入された実行時の権限に明らかに起因し、cordova-androidは少しクリーンアップされるまで、問題に対処することができますプラグインがあります(11/6/2017現在、PhoneGap Buildはcordoby-android 6.2.3を使用しています。これはcordova-plugin-compatに依存しています)。今の私の修正:

cordova plugin add cordova.plugins.diagnostic 

そして、適切なランタイム権限を追加するルーチン:アンドロイド6について

// request read access to the external storage if we don't have it 

cordova.plugins.diagnostic.getExternalStorageAuthorizationStatus(function (status) { 
    if (status === cordova.plugins.diagnostic.permissionStatus.GRANTED) { 
     console.log("External storage use is authorized"); 
    } else { 
     cordova.plugins.diagnostic.requestExternalStorageAuthorization(function (result) { 
      console.log("Authorization request for external storage use was " + (result === cordova.plugins.diagnostic.permissionStatus.GRANTED ? "granted" : "denied")); 
     }, function (error) { 
      console.error(error); 
     }); 
    } 
}, function (error) { 
    console.error("The following error occurred: " + error); 
}); 
関連する問題