Phonegap Android本地文件系统未定义。

11

我在我的phonegap应用程序中访问Android文件系统的存储目的有问题。我已经搜索并找到了很多与我的情况不符的解决方案!

我知道必须触发和检查deviceready,它已经被触发。因此,phonegap.js已经完美加载!

我认为我请求了文件权限并在我的清单和配置文件中具有功能。以下是它们:

config.xml:

    <feature name="http://api.phonegap.com/1.0/device" />
    <feature name="http://api.phonegap.com/1.0/battery"/>
    <feature name="http://api.phonegap.com/1.0/camera"/>
    <feature name="http://api.phonegap.com/1.0/contacts"/>
    <feature name="http://api.phonegap.com/1.0/file"/>
    <feature name="http://api.phonegap.com/1.0/geolocation"/>
    <feature name="http://api.phonegap.com/1.0/media"/>
    <feature name="http://api.phonegap.com/1.0/network"/>
    <feature name="http://api.phonegap.com/1.0/notification"/>
    <preference name="permissions" value="none" />
    <preference name="orientation" value="default" />
    <preference name="target-device" value="universal" />
    <preference name="fullscreen" value="true" />
    <preference name="webviewbounce" value="true" />
    <preference name="prerendered-icon" value="true" />
    <preference name="stay-in-webview" value="false" />
    <preference name="ios-statusbarstyle" value="black-opaque" />
    <preference name="detect-data-types" value="true" />
    <preference name="exit-on-suspend" value="false" />
    <preference name="show-splash-screen-spinner" value="true" />
    <preference name="auto-hide-splash-screen" value="true" />
    <preference name="disable-cursor" value="false" />
    <preference name="android-minSdkVersion" value="7" />
    <preference name="android-installLocation" value="auto" />
    <icon src="icon.png" />
    <icon gap:density="ldpi" gap:platform="android" src="res/icon/android/icon-36-ldpi.png" />
    <icon gap:density="mdpi" gap:platform="android" src="res/icon/android/icon-48-mdpi.png" />
    <icon gap:density="hdpi" gap:platform="android" src="res/icon/android/icon-72-hdpi.png" />
    <icon gap:density="xhdpi" gap:platform="android" src="res/icon/android/icon-96-xhdpi.png" />
    <access origin="*" />
    <content src="index.html" />
</widget>

AndroidManifest.xml:

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="1.0.0" android:windowSoftInputMode="adjustPan" package="com.pakhshyaran.kara" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.BROADCAST_STICKY" />
    <uses-permission android:name="android.permission." />
    <application android:debuggable="true" android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:name="Kara" android:theme="@android:style/Theme.Black.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="18" />
</manifest>

这是我代码中抛出错误的部分:

$(document).ready(function () {
    document.addEventListener("deviceready", onDeviceReady, false);
});
function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) { //throws the 'Uncaught ReferenceError: LocalFileSystem is not defined' error
        fileSystem = fileSystem;
    }, function (evt) {
        alert(evt.target.error.code);
    });
}

但是我因为一个我完全不了解的原因而出现了错误!请给我您的想法。非常感谢。

PS:我正在使用phonegap 3.0!

1个回答

19

你是否在使用PhoneGap CLI构建项目?那么你可能错过了将文件插件添加到项目中。除了在 config.xmlAndroidManifiest.xml 中进行配置之外,你还需要该插件的代码。你可以通过以下方式将其添加到项目中:

$ phonegap plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-file.git 

请查看文档页面顶部:http://docs.phonegap.com/en/3.0.0/cordova_file_file.md.html#LocalFileSystem

你也可以检查文件android/assets/www/cordova_plugins.js,以查看window.LocalFileSystem是否已定义。 寻找类似于以下内容的内容:

{
    "file": "plugins/org.apache.cordova.core.file/www/FileSystem.js",
    "id": "org.apache.cordova.core.file.FileSystem",
    "clobbers": [
        "window.FileSystem"
    ]
},

2
谢谢您的回复。我不知道我必须添加插件。我运行了命令,它被执行了。但是cordova_plugins.js没有发生任何变化。还有其他需要注意的事项吗? - Hosein
2
@Hosein 添加插件实际上也会添加配置。尝试重新构建它:cordova build android - davidlgj
1
安装插件有两个步骤:1)使用CLI安装插件:“cordova plugin add org.apache.cordova.file-transfer” - 这将更新AndroidManifest.xml和res/xml/config.xml。2)执行“cordova build android” - 这将把必要的JS插件文件写入www/plugins。 - Kozuch

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接