如何在手机内存中保存文件?

时间:2013-08-05 07:36:16

标签: android cordova

您好我正在制作一个Android应用程序起诉phonegap.Here我必须将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");
       }

提前致谢!

2 个答案:

答案 0 :(得分:1)

一些提示:

  • 避免使用file:// sdcard /使用字符串路径= Environment.getExternalStorageDirectory()。getAbsolutePath(); 而不是
  • 在我的手机中,即使没有SD卡,上面的提示仍可正常工作,但如果您想尝试其他替代方法,请尝试使用 String path = Environment.getDataDirectory()。getAbsolutePath();
  • 请记住更改de Android Manifest:
  

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

答案 1 :(得分: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");

注意:请务必将插件添加到PhoneGap的config.xml文件中。此外,这是使用PhoneGap 2.8,对于旧版本,请参考documentation