我寫了一個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();
}
}
};
是否使用文本文件中的特定API函數? – mkl
@mkl即時通訊使用文件同步api支持基於與sdk給出的例子。我不知道任何基於文件類型的特殊api。基於文件類型的dropbox是否有特殊的API,我錯過了? – Thilek
@mkl你是對的..我正在使用一個錯誤的api ..我應該使用核心API,而不是同步api的PDF或圖像文件..感謝隊友... – Thilek