2016-09-05 21 views
0

このパス/ storage/emulated/0/DCMI/CameraのAndroidデバイスのCameraディレクトリにアクセスしたいとします。 このチュートリアルのhttp://nightlycoding.com/index.php/2015/06/phonegapcordova-read-images-from-gallery-folder-tip/には、Cordova File Plugin(https://github.com/apache/cordova-plugin-file)を使用しています。 問題は、Android 4.3でアプリを実行した場合、これらのファイルにアクセスできますが、Android Lollipopデバイスでアプリをテストすると、そのディレクトリ内のファイル/ディレクトリは空になります。Android LollipopのCordovaファイルプラグイン

私はあなたにここに

/* 
* Licensed to the Apache Software Foundation (ASF) under one 
* or more contributor license agreements. See the NOTICE file 
* distributed with this work for additional information 
* regarding copyright ownership. The ASF licenses this file 
* to you under the Apache License, Version 2.0 (the 
* "License"); you may not use this file except in compliance 
* with the License. You may obtain a copy of the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, 
* software distributed under the License is distributed on an 
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
* KIND, either express or implied. See the License for the 
* specific language governing permissions and limitations 
* under the License. 
*/ 
var app = { 
    // Application Constructor 
    initialize: function() { 
     this.bindEvents(); 
    }, 
    // Bind Event Listeners 
    // 
    // Bind any events that are required on startup. Common events are: 
    // 'load', 'deviceready', 'offline', and 'online'. 
    bindEvents: function() { 
     document.addEventListener('deviceready', this.onDeviceReady, false); 
    }, 
    // deviceready Event Handler 
    // 
    // The scope of 'this' is the event. In order to call the 'receivedEvent' 
    // function, we must explicitly call 'app.receivedEvent(...);' 
    onDeviceReady: function() { 
     window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, onFileSystemSuccess, function(){alert("fail");}); 
    }, 
}; 
//app.mediaFiles = []; 

function onFileSystemSuccess(fileSystem) { 
    window.console.log(cordova.file.externalRootDirectory); 
    window.console.log(JSON.stringify(fileSystem)); 
    var directoryReader = fileSystem.createReader(); 
    alert(JSON.stringify(directoryReader)); //1 
    directoryReader.readEntries(function (entries) { 
     var i; 
     alert(JSON.stringify(entries)); //2 
     for (i = 0; i < entries.length; i++) { 
      if (entries[i].name === "DCIM") { 
       var dcimReader = entries[i].createReader(); 
       dcimReader.readEntries(onGetDCIM, fail); 
       break; // remove this to traverse through all the folders and files 
      } 
     } 
    }, function() { 
     window.console.log("fail"); 
    }); 
} 

function onGetDCIM(entries) { 
    var i; 
    for (i = 0; i < entries.length; i++) { 
     if (entries[i].name === "Camera") { 
      var mediaReader = entries[i].createReader(); 
      mediaReader.readEntries(onGetFileNames, fail); 
      break; // remove this to traverse through all the folders and files 
     } 
     //This will log all files and directories inside 100MEDIA 
     alert(" >>>>>>> " + entries[i].name); 
    } 
} 

function onGetFileNames(entries) { 
    var i; 
    var flag=true; 
    console.log("files"); 
    for (i = 0; i < entries.length; i++) { 
     if (/\.(jpe?g|png|gif|bmp)$/i.test(entries[i].name)) { 
      if (flag){ 
      alert(entries[i].nativeURL); 
      mostrarImagen(entries[i].nativeURL); 
      flag=false; 
      } 
      //app.mediaFiles.push(entries[i]); 
      //alert(JSON.stringify(entries[i])); 
     } 
     //This will log all image files found 
    } 
} 
function mostrarImagen(ImagePath){ 
    alert("mostrar la foto " + ImagePath); 
    var imagen = new Image(); 
    imagen.src = ImagePath; 
    document.getElementsByName("foto").src= imagen.src; 

} 
app.initialize(); 

コード "onFileSystemSuccess" 機能ショー{ "は、localUrl": "cdvfile:// localhostの/ SDカード/"、 "hasReadEntries":偽}の最初の警告をしましょう。 アンドロイド4.3では、2番目のアラートには、そのパス上のサブディレクトリのリストが表示されますが、Android 5ではそのリストは空です。

いくつかの解決策はありますか? Android Lollipopデバイスの外部ファイルにどうすればアクセスできますか?

答えて

0

セキュリティポリシーの更新に関する問題が発生している可能性があります(詳細については、the cordova-plugin-file docsを参照してください)。これらは新しいもので、アプリをより安全にする一方、私はまだすべての問題をキャッチしようとしています。チェックするカップルの事:

  • index.htmlあなたがアクセスすることがありますもののために適切なコンテンツ・セキュリティ・ポリシーのメタタグを持っている必要があります。ドキュメントには、出発点としてこれをお勧めします:答えるためこんにちは!おかげで、私が使用してこの問題を解決することができ

    <access origin="*" />

+1

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

  • ​​3210は、ローカルアクセスを許可する必要がありますAndroidのアクセス許可を明示的に指定します。Androidの最新バージョンでは、明示的に許可を与える必要があります。 –

  • 関連する問題