2013-08-05 85 views
1

嗨我正在做一個android phone應用程序起訴phone.Here我必須將pdf文檔保存在手機內存memory.But它是存儲在手機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"); 
     } 

謝謝!

回答

1

一些提示:

  • 避免使用文件:// SD卡/使用字符串路徑= Environment.getExternalStorageDirectory()getAbsolutePath();而不是
  • 在我的手機中,即使沒有SD卡,上面的提示也能正常工作,但如果您想嘗試另一種替代方法,請嘗試使用String path = Environment.getDataDirectory()。getAbsolutePath();
  • 記住更改德Android清單:

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

+0

Android Manifest需要哪些改變?我有同樣的問題 –

+0

'

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的​​3210文件的插件。此外,這是使用PhoneGap 2.8,舊版本參考documentation

相關問題