2014-02-20 49 views
0

我寫了一個Android應用程序,它通過電子郵件和Dropbox導出PDF文件。當我發送PDF文件使用電子郵件意圖,我可以查看我的Mac上的文件..但同樣的文件上傳到Dropbox通過android Dropbox API,PDF文件似乎是空的,當我打開我的Mac。但Dropbox版本和電子郵件版本的文件大小相同。這隻發生在PDF文件,當我發送一個文本文件通過Dropbox的Android API,它工作正常。同步後使用Dropbox API在同步後的空白PDF文件

之前有人遇到過這樣的問題嗎?並有任何解決方案?我使用Dropbox開發者方面最新的Dropbox android sdk。

public DropboxExportManager(Activity context) { 
     this.context = context; 
    } 


    private void setUpAccountDetail() { 
     handler = new Handler(Looper.getMainLooper()); 
     progressDialog = new ProgressDialog(context); 
     progressDialog 
       .setMessage(context.getString(R.string.uploading_message)); 

     dropBoxAccountManager = DbxAccountManager.getInstance(
       context.getApplicationContext(), PropertyManager.getInstance().getString(
       PropertyKeys.DROP_BOX_KEY), PropertyManager.getInstance().getString(
       PropertyKeys.DROP_BOX_SECRET)); 
    } 

    public void connectToDropbox(String file) { 
     setUpAccountDetail(); 

     this.dropBoxFile = file; 

     if (dropBoxAccountManager.hasLinkedAccount()) { 
      uploadFileToDropBox(); 
     } else { 
      dropBoxAccountManager.startLink(context, REQUEST_LINK_TO_DROPBOX); 

     } 

    } 

    public void uploadFileToDropBox() { 

     try { 

      DbxPath dropboxFilePath = new DbxPath(DbxPath.ROOT, directory + dropBoxFile); 

      DbxFileSystem dropBoxFileSystem = DbxFileSystem 
        .forAccount(dropBoxAccountManager.getLinkedAccount()); 

      File localFile = new File(FileService.getInstance() 
        .getPathAndCreateIfNotExists(FileService.EXTERNAL, 
          AppConstants.CACHE_DIRECTORY + dropBoxFile)); 

      dropBoxFileSystem.addPathListener(pathListener, dropboxFilePath, 
        Mode.PATH_ONLY); 

      if (localFile.exists()) { 
       Logs.d("Local file exist"); 
       DbxFile dropboxFile; 
       if (!dropBoxFileSystem.exists(dropboxFilePath)) { 
        Logs.e("Dropbox file doesnt exist"); 
        dropboxFile = dropBoxFileSystem.create(dropboxFilePath); 
        writeDropBoxFileFromFile(localFile, dropboxFile); 
       } else { 
        Logs.d("Dropbox file exist"); 
        dropBoxFileSystem.delete(dropboxFilePath); 
        dropboxFile = dropBoxFileSystem.create(dropboxFilePath); 
        writeDropBoxFileFromFile(localFile, dropboxFile); 
       } 

       if (dropboxFile != null) { 
        dropboxFile.close(); 
       } 

       if (exportManager != null) { 
        exportManager.onExportComplete(); 
       } 

      } else { 
       Logs.e("Local file doesnt exist"); 
      } 

     } catch (IOException e) { 
      statusMessage = "Dropbox test failed: " + e; 
     } 

     Logs.d(statusMessage); 
    } 

    public boolean writeDropBoxFileFromFile(File file, DbxFile dbFile) { 
     try { 
      FileInputStream fin = new FileInputStream(file); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        fin)); 
      String line = null; 
      StringBuilder sb = new StringBuilder(); 
      while ((line = reader.readLine()) != null) { 
       sb.append(line).append("\n"); 
      } 

      dbFile.writeString(sb.toString()); 

      fin.close(); 
     } catch (Exception e) { 
      return false; 
     } finally { 
      dbFile.close(); 

     } 

     return true; 

    } 

    private DbxFileSystem.PathListener pathListener = new DbxFileSystem.PathListener() { 

     @Override 
     public void onPathChange(DbxFileSystem dbFS, DbxPath dbPath, Mode arg2) { 
      try { 

       Logs.d("Uploading File " 
         + dbFS.getSyncStatus().upload.inProgress); 

       if (dbFS.getSyncStatus().upload.inProgress) { 
        handler.post(new Runnable() { 
         public void run() { 
          progressDialog.show(); 
         } 
        }); 

       } else { 
        handler.post(new Runnable() { 
         public void run() { 
          progressDialog.dismiss(); 
         } 
        }); 

        dbFS.removePathListenerForAll(pathListener); 
       } 

      } catch (DbxException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    }; 
+1

是否使用文本文件中的特定API函數? – mkl

+0

@mkl即時通訊使用文件同步api支持基於與sdk給出的例子。我不知道任何基於文件類型的特殊api。基於文件類型的dropbox是否有特殊的API,我錯過了? – Thilek

+0

@mkl你是對的..我正在使用一個錯誤的api ..我應該使用核心API,而不是同步api的PDF或圖像文件..感謝隊友... – Thilek

回答

1

至於爲什麼你的代碼不能正常工作,我覺得你上面的評論是正確的,因爲它涉及到試圖讀取該文件,就好像它是文本。 (至少,你可能會在文件中寫入幾行換行符。)

但是,實際上並不需要代碼。而不是調用writeDropBoxFileFromFile方法你寫的,只是做dropboxFile.writeFromExistingFile(localFile, false);

(該文檔是在這裏:https://www.dropbox.com/developers/sync/docs/android#com.dropbox.sync.android.DbxFile只要看看「writeFromExistingFile」。)

0

最後我找出問題所在。在閱讀有關apis的更多信息之前,我很害怕跳進程序設計。看起來像保管箱有兩種類型的apis。一種用於使用同步pi的正常文本寫入,另一種用於使用核心api上傳文件等更復雜的任務。

Core API

+0

這絕對是**錯**。這兩種API對文本文件和非文本文件同樣適用。例如,Sync API帶有一個ImageGrid示例,它顯示了使用圖像文件的情況。如果您仍然對使用Sync API感興趣,並想知道您的代碼出了什麼問題,請分享代碼,以便我們嘗試發現錯誤。 – smarx

+0

@smarx這是我第二次嘗試使用這個api ..我不知道它是如何工作的..我添加了上面的代碼。但我想現在我知道它出了什麼地方..我想使用stringer生成器時寫入文件...這不適用於非文本文件..在此先感謝:) – Thilek