如何使用Eclipse中的Phonegap在移动目录中创建简单的文本文件

时间:2015-07-24 09:30:19

标签: javascript android eclipse cordova phonegap-plugins

我试图使用phonegap在Android存储中创建一个简单的文本文件。 但是onLoad方法并没有被自己调用。 所以我尝试使用按钮手动调用方法。

  

点击onLoad方法的按钮我在logcat中获取此信息   

这意味着也不会调用 onDeviceReady 方法。然后我试着用按钮调用它。然后我明白了:

它说window.requestFileSystem未定义

请帮我解决这个问题

这是我的 index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Log file</title>

<script type="text/javascript" charset="utf-8" src="phonegap-1.3.0.js"></script>

<script type="text/javascript" charset="utf-8" src="jquery.min.js"></script>
<script type="text/javascript" charset="utf-8" src="cordova-1.8.0.js"></script>

<script type="text/javascript" charset="utf-8">

        // Wait for PhoneGap to load


        function onLoad() 
        {

         document.addEventListener("deviceready", onDeviceReady, false);
         console.log("console onload called");
        }

        // PhoneGap is ready

        function onDeviceReady() 
        {
          console.log("aaa onDeviceReady Called before FS "+window.requestFileSystem);

          window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, gotFS, fail);

          console.log("aaa onDeviceReady Called after FS "+window.requestFileSystem );

        }

        function gotFS(fileSystem) {
         console.log("console gotFS called" + fileSystem);
          alert('gotFS Called '+fileSystem); 
         fileSystem.root.getFile("hello", {create: true}, gotFileEntry, fail);
         alert('if Fail '+ fail);
        }

         function gotDir(dirEntry) {

         alert('gotDir Called '+ dirEntry);

         console.log("console gotDir called" + fileSystem);

         dirEntry.getFile("login.txt", {create: true, exclusive: true}, gotFile);


        }


        function gotFileEntry(fileEntry) {

        console.log("console gotFileEntry Called" );
        fileEntry.createWriter(gotFileWriter, fail);

        }

        function gotFileWriter(writer) {

        writer.onwrite = function(evt) {
        console.log("console gotFileWriter Called" );

         };

            writer.write("some sample text");
            writer.abort();
            // contents of file now 'some different text'
        }

        function fail(error) {
            console.log("console error : "+error.code);
        }


 </script>
 </head>
 <body >
 <button id="button" type="button" onClick="onLoad();">one onLoad</button>
 <button id="button" type="button" onClick="onDeviceReady();">one onDeviceReady</button>
 <button id="actionOne" type="button" onClick="gotFS();">two getFileSystem</button>
 <button id="actionTwo" type="button" onClick="gotFileEntry();">three gotFileEntry</button>
 <button id="actionTwo" type="button" onClick="gotFileWriter();">four gotFileWriter</button>
 <button id="actionTwo" type="button" onClick="gotDir();">five gotDir</button>
 <button id="actionTwo" type="button" onClick="gotFileEntry();">six gotFileEntry</button>
 <button id="actionTwo" type="button" onClick="gotFileWriter();">seven gotFileWriter</button>

</body>
</html>
  

这是我的项目结构:

  

这是我的 Config.xml

  <?xml version="1.0" encoding="UTF-8"?>
<widget xmlns     = "http://www.w3.org/ns/widgets"
    xmlns:gap   = "http://phonegap.com/ns/1.0"
    id        = "io.cordova.helloCordova"
    version   = "2.0.0">
<name>Hello Cordova</name>

<description>
    A sample Apache Cordova application that responds to the deviceready event.
</description>

<author href="http://cordova.io" email="dev@cordova.apache.org">
    Apache Cordova Team
</author>

<access origin="*"/>


<content src="index.html" />

<preference name="loglevel" value="DEBUG" />
<!--
  <preference name="splashscreen" value="resourceName" />
  <preference name="backgroundColor" value="0xFFF" />
  <preference name="loadUrlTimeoutValue" value="20000" />
  <preference name="InAppBrowserStorageEnabled" value="true" />
  <preference name="disallowOverscroll" value="true" />
-->

<feature name="App">
  <param name="android-package" value="org.apache.cordova.App"/>
</feature>
<feature name="Geolocation">
  <param name="android-package" value="org.apache.cordova.GeoBroker"/>
</feature>
<feature name="Device">
  <param name="android-package" value="org.apache.cordova.Device"/>
</feature>
<feature name="Accelerometer">
  <param name="android-package" value="org.apache.cordova.AccelListener"/>
</feature>
<feature name="Compass">
  <param name="android-package" value="org.apache.cordova.CompassListener"/>
</feature>
<feature name="Media">
  <param name="android-package" value="org.apache.cordova.AudioHandler"/>
</feature>
<feature name="Camera">
  <param name="android-package" value="org.apache.cordova.CameraLauncher"/>
</feature>
<feature name="Contacts">
  <param name="android-package" value="org.apache.cordova.ContactManager"/>
</feature>
<feature name="File">
  <param name="android-package" value="org.apache.cordova.FileUtils"/>
</feature>
<feature name="NetworkStatus">
  <param name="android-package" value="org.apache.cordova.NetworkManager"/>
</feature>
<feature name="Notification">
  <param name="android-package" value="org.apache.cordova.Notification"/>
</feature>
<feature name="Storage">
  <param name="android-package" value="org.apache.cordova.Storage"/>
</feature>
<feature name="FileTransfer">
  <param name="android-package" value="org.apache.cordova.FileTransfer"/>
</feature>
<feature name="Capture">
  <param name="android-package" value="org.apache.cordova.Capture"/>
</feature>
<feature name="Battery">
  <param name="android-package" value="org.apache.cordova.BatteryListener"/>
</feature>
<feature name="SplashScreen">
  <param name="android-package" value="org.apache.cordova.SplashScreen"/>
</feature>
<feature name="Echo">
  <param name="android-package" value="org.apache.cordova.Echo"/>
</feature>
<feature name="Globalization">
  <param name="android-package" value="org.apache.cordova.Globalization"/>
</feature>
<feature name="InAppBrowser">
  <param name="android-package" value="org.apache.cordova.InAppBrowser"/>
</feature>
<gap:plugin name="org.apache.cordova.file" />
<gap:plugin name="org.apache.cordova.FileTransfer" />
<gap:plugin name="org.apache.cordova.FileUtils" />

</widget>
  

这是我的 Plugin.xml

<?xml version="1.0" encoding="utf-8"?>
<widget>
<!-- <feature name="File" value="org.apache.cordova.FileUtils" >
</feature>
<feature name="FileTransfer" value="org.apache.cordova.FileTransfer">
</feature> -->

</widget>
  

这是我的 Menifest文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:windowSoftInputMode="adjustPan"
  package="org.apache.cordova.example" android:versionName="1.0" android:versionCode="1" android:hardwareAccelerated="true">
<supports-screens
    android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:xlargeScreens="true"
    android:resizeable="true"
    android:anyDensity="true"
    />

<uses-permission android:name="android.permission.CAMERA" />
<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.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO"/>
<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.WRITE_EXTERNAL_STORAGE" />   
<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.BLUETOOTH" />



<application android:icon="@drawable/icon" android:label="@string/app_name"
    android:hardwareAccelerated="true"
     >
    <activity android:name="example" android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="17"/>

如果我需要下载或添加内容,请告诉我

1 个答案:

答案 0 :(得分:1)

你的尝试很棒,但我觉得你觉得有点复杂。正如我在您的代码中看到的那样,您做到了,Cordova文档告诉您的内容。文档很棒,但有时候,是的,有点复杂。

要使deviceReady州工作,我不会在onload标记上设置body个事件。就这样做,它就像一个魅力。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <script type="text/javascript" src="js/xtndr.js"></script>
        <script type="text/javascript" src="cordova.js"></script>
        <title>Hello World</title>
    </head>
    <script>
        function onDeviceReady() {
            alert('Device is ready'); 
        }

        document.addEventListener("deviceready", onDeviceReady, false)
    </script>

    <body>
    </body>
</html>

请注意

我建议大家永远不要在console.log()事件中使用deviceReady。根据您使用的调试器,可能会在控制台输出准备好之前触发console.log()。因此,请使用alert()因为每次代码都会触发警报并直接显示在您的设备上!