问题
I've got what appears to be a file read permissions issue in my Cordova app, but I can't for the life of me track it down properly. Between Android 5 and Android 6, I'm seeing completely different results to the same calls to fileEntry
. Does anyone know whether there are needed changes to be able to read files/directories in Android > 5?
Gory details: I copied the code out to a helloWorld app with just cordova-plugin-file
(calling cordova create hello
), and am getting the same results. My changes are as follows:
I added a single element to the body of index.html
:
<div id="contents"></div>
I also changed the stock receivedEvent method in index.js
, just to iterate through the various directories on the device and return what cordova-plugin-file
gives me:
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);
}
Possible security configurations:
My platforms/android/AndroidManifest.xml
file contains the appropriate permissions:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Here's my CSP in index.html (it's just the stock one):
<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:;">
Here are the results:
Android 5. Note directories and a file (there are others offscreen)
Android 6. Note that I have some files in the subdirectories under cordova.file.externalRootDirectory
that should be displayed; it should also be displaying the subdirectories themselves.
回答1:
Hat tip to @greenapps for the hint -- this behavior is apparently due to the runtime permissions introduced in Android 6.
For Cordova, there's a plugin that can address the issue until cordova-android
is cleaned up a bit (as of 11/6/2017, PhoneGap Build uses cordova-android 6.2.3, which depends on cordova-plugin-compat
to deal with this). My fix for now:
cordova plugin add cordova.plugins.diagnostic
Then the routine to add the appropriate runtime permissions:
// 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);
});
来源:https://stackoverflow.com/questions/47143656/no-fileentry-results-returned-on-android-6