1

こんにちは、phonegap を訴える Android アプリケーションを作成しています。ここでは、pdf ドキュメントを電話の内部メモリに保存する必要があります。PDFファイルを電話の内部メモリに保存するのを手伝ってください私のコードは

   var filename = "read-write.txt";
         var filePath = "file:///sdcard/read-write.txt";

function saveFile(){
           window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccessSave,onErrorSave);     
       }
       function onSuccessSave(fileSystem){
           var sdcardEntry = fileSystem.root;
           sdcardEntry.getFile(filename,{create: true}, gotFileEntry, failEntry);
       }
       function gotFileEntry(fileEntry){
           fileEntry.createWriter(gotFileWriter, failWrite);
       }
       function gotFileWriter(fileWriter){
           fileWriter.onwrite = function (evt) {
               alert("Write was successful!");
               document.getElementById("textarea").value = "";
           };
           fileWriter.write(document.getElementById("textarea").value);
       }
       function failWrite(error){
           alert("Failed to get a file writer for " + filename); 
       }
       function failEntry(error){
           alert("Got error while reading " + filename + " " + error);
       }
       function onErrorSave(error){
           alert("Got Error while gaining access to file system");
       }

前もって感謝します!

4

2 に答える 2

0

PhoneGap 用の独自のプラグインを開発し、ファイルを保存するためのネイティブ サイド メソッドを直接制御できます。

最初に JavaScript プラグインを開発します。

var FileSaver = {   
};

FileSaver.saveFile = function(successCallback, errorCallback, fileName) {
    cordova.exec(successCallback, errorCallback, "FileSaverPlugin", "saveFile", [fileNameToSave, filePathOfPdf]);
};

次に、FileSaverPlugin.java というクラスを追加します。

public class FileSaverPlugin extends CordovaPlugin
{
    private CallbackContext callbackContext = null;

    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException 
    {
            this.callbackContext = callbackContext;

            if (action.equals("saveFile")) 
            {
                this.saveFile(args.getString(0), args.getString(1));
            }
            else
            {
                return false;
            }

            return true;
    }

    public void saveFile(String fileNameToSave, String filePathOfPdf)
    {
        FileOutputStream outputStream = null;
        File file = new File(filePathOfPdf);
        byte[] byteArray = this.readFileToByteArray(file);

        try 
        {
            // Saves the pdf to the internal phone memory.
            outputStream = cordova.getActivity().openFileOutput(fileNameToSave, Context.MODE_PRIVATE);
            outputStream.write(byteArray);
            outputStream.close();
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

    private byte[] readFileToByteArray(File file)
    {
        byte[] byteArray = new byte[(int) file.length()];

        try 
        {
            BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(file));
            buffer.read(byteArray, 0, byteArray.length);
            buffer.close();
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }

        return byteArray;
    }
}

したがって、ファイルを保存する必要がある場合は、次の JavaScript メソッドを使用するだけです。

FileSaver.saveFile("name-of-file-save-in-internal-memory.pdf", "/path/to/pdf/aDocument.pdf");

config.xml: プラグインをPhoneGapのファイルに必ず追加してください。また、これは PhoneGap 2.8 を使用しています。古いバージョンについては、ドキュメントを参照してください。

于 2013-08-05T10:58:20.180 に答える