Reputation: 676
i wrote an android app which exports pdf file over email and dropbox. When i send the pdf file using email intent, i can view the file on my mac.. But the same file uploaded into dropbox via android dropbox api, the pdf file seems to be empty when i open on my mac. But the file size of both dropbox version and email version are the same. This only happens for pdf files, when i send a text file over dropbox android api, it works fine.
Anyone experienced such issue before ? and are there any solutions ? i'm using the latest dropbox android sdk from dropbox developer side.
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();
}
}
};
Upvotes: 0
Views: 499
Reputation: 60143
As to why your code doesn't work, I think your comment above is right in that it relates to trying to read the file as though it's text. (At the very least, you're probably writing a couple newlines into the file.)
But there's really no need for that code anyway. Instead of calling the writeDropBoxFileFromFile
method you wrote, just do dropboxFile.writeFromExistingFile(localFile, false);
(The docs are here: https://www.dropbox.com/developers/sync/docs/android#com.dropbox.sync.android.DbxFile. Just look for "writeFromExistingFile
".)
Upvotes: 1
Reputation: 676
Finally i figure out the problem. It was so silly of me to jump into programming before read more about the apis. Seems like dropbox has two type of apis. One for normal text writing using the sync pi and another for more complex task like uploading files using the core api.
Upvotes: 0