简体   繁体   中英

read a text file from sdcard and display using phone gap

I have example.txt file sdcard and I want to display it using phone gap.I am very new to android and phone gap please help. Here is my example.txt file:

     <h2>This file is in sdcard</h2>

Here is my phonegap.java

    package com.example.phonegap;
    import org.apache.cordova.DroidGap;

     public class Phonegap extends DroidGap{
        /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/myfile.html");
       }

    }

and here is the myfile.html in www:

       <!DOCTYPE html>
     <html>
     <head>
      <title>FileReader Example</title>

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

    // Wait for PhoneGap to load
    //
    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }

    // PhoneGap is ready
    //
    function onDeviceReady() {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
    }

    function gotFS(fileSystem) {
        fileSystem.root.getFile("file:///sdcard/example.txt", {create: true}, gotFileEntry, fail);
    }

    function gotFileEntry(fileEntry) {
        fileEntry.file(gotFile, fail);
    }

    function gotFile(file){
        readDataUrl(file);
        readAsText(file);
    }

    function readDataUrl(file) {
        var reader = new FileReader();
        reader.onloadend = function(evt) {
            console.log("Read as data URL");
            console.log(evt.target.result);
        };
        reader.readAsDataURL(file);
    }

    function readAsText(file) {
        var reader = new FileReader();
        reader.onloadend = function(evt) {
            console.log("Read as text");
            console.log(evt.target.result);
        };
        reader.readAsText(file);
    }

    function fail(evt) {
        console.log(evt.target.error.code);
    }

    </script>
  </head>
  <body>
    <h1>Example</h1>
    <p>Read File</p>
  </body>
</html>

Change this line:

fileSystem.root.getFile("file:///sdcard/example.txt", {create: true}, gotFileEntry, fail);

to be this:

fileSystem.root.getFile("example.txt", {create: false}, gotFileEntry, fail);

assuming that the file you want to read is "example.txt" and is located in the /sdcard folder.

I also encountered the same problem. I don't think the API document in Phonegap is right, so i referred JavaScript:the definitive guide , I found two key points:

  1. the location of the file I am ready to read. --> I found the default parent path is: /mnt/sdcard. So "/mnt/sdcard/temp/test.txt" file was written as fileSystem.root.getFile("temp/test.txt", null, gotFileEntry, fail)

  2. after reader.readAsText(file) , just using element.innerHTML = reader.result can't display the result in HTML file. so I add a callback:

     reader.onload = function(){ element.innerHTML = reader.result }

After adjusting the two parts, my code works!

StackOverflow has very strict upload format policy, so I can't upload the exact code. But you can contact me to ask for the code.

I assume that the example.txt is at the root of SD card(Not resides in a folder). Then change following of your code,

function gotFS(fileSystem) {
        fileSystem.root.getFile("file:///sdcard/example.txt", {create: true}, gotFileEntry, fail);
    }

to

function gotFS(fileSystem) {
        fileSystem.root.getFile("example.txt", {create: true}, gotFileEntry, fail);
    }

And try by using alert in the below function,

function readAsText(file) {
        alert("readAstext");
        var reader = new FileReader();
        reader.onloadend = function(evt) {


               console.log("Read as text");
                console.log(evt.target.result);
                alert("content : "+evt.target.result);
            };
            reader.readAsText(file);
}

If that is not work, please add below code to your body tag in HTML

 <body onload='onLoad();'>

Let me know issues you may faced. Thanks,,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM