【问题标题】:Cordova File Plugin on Android LollipopAndroid Lollipop 上的 Cordova 文件插件
【发布时间】:2017-01-12 23:54:48
【问题描述】:

我想访问此路径 /storage/emulated/0/DCMI/Camera 中的 Android 设备中的相机目录。 我遵循了使用 Cordova 文件插件 https://github.com/apache/cordova-plugin-file 的本教程 http://nightlycoding.com/index.php/2015/06/phonegapcordova-read-images-from-gallery-folder-tip/。 问题是,如果我在 Android 4.3 上运行应用程序,我可以访问这些文件,但是当我在 Android Lollipop 设备上测试应用程序时,该目录中的文件/目录是空的。

我在这里给你代码

 /*
 * 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/sdcard/","hasReadEntries":false}。 在 android 4.3 中,第二个警报显示该路径上的子目录列表,但在 Android 5 中,该列表为空。

一些解决方案?如何访问 Android Lollipop 设备上的外部文件?

【问题讨论】:

    标签: android cordova filesystems cordova-plugins


    【解决方案1】:

    您可能遇到了安全策略更新问题(请参阅the cordova-plugin-file docs 了解更多信息)。这些是较新的,虽然它们使应用程序更安全,但我仍在努力解决所有问题。需要检查的几件事:

    • index.html 需要为您将要访问的内容设置适当的 content-security-policy 元标记。文档建议将此作为起点:

      &lt;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 *"&gt;

    • config.xml 必须允许本地访问:

      &lt;access origin="*" /&gt;

    【讨论】:

    • 您好!感谢您的回答,我可以使用 Android Permission 明确解决此问题,对于最新版本的 Android,您需要明确授予权限。
    猜你喜欢
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多