1

嗨,我正在制作一个起诉 phonegap 的 android 应用程序。这里我必须将 pdf 文档保存在手机内部存储器中。但它保存在手机 SD 卡中。请帮我将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

1

一些技巧:

  • 避免使用 file://sdcard/ 使用String path = Environment.getExternalStorageDirectory().getAbsolutePath(); 反而
  • 在我的手机中,即使没有 SD 卡,上述提示也有效,但如果您想尝试另一种选择,请尝试使用String path = Environment.getDataDirectory().getAbsolutePath();
  • 请记住更改 de Android Manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

于 2013-08-05T07:59:37.483 回答
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.xmlPhoneGap 的文件中。此外,这是使用 PhoneGap 2.8,对于旧版本,请参阅文档

于 2013-08-05T10:58:20.180 回答