簡體   English   中英

Android Lollipop上的Cordova文件插件

[英]Cordova File Plugin on Android Lollipop

我想在此路徑/ storage / emulated / 0 / DCMI / Camera中訪問Android設備中的Camera目錄。 我遵循了本教程http://nightlycoding.com/index.php/2015/06/phonegapcordova-read-images-from-gallery-folder-tip/ ,該教程使用了Cordova文件插件, https://github.com/apache/ cordova-plugin-file 問題是,如果我在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設備上的外部文件?

您可能遇到了安全策略更新問題(有關更多信息,請參閱cordova-plugin-file文檔 )。 這些是較新的,盡管它們使應用程序更安全,但我仍在嘗試抓住所有問題。 需要檢查的幾件事:

  • index.html需要具有一個適當的content-security-policy元標記來處理您將要訪問的內容。 文檔建議將此作為起點:

    <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 *">

  • config.xml必須允許本地訪問:

    <access origin="*" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM