在android上使用dropbox api进行同步后清空pdf文件

在android上使用dropbox api进行同步后清空pdf文件,android,pdf,dropbox-api,Android,Pdf,Dropbox Api,我写了一个android应用程序,它通过电子邮件和dropbox导出pdf文件。当我使用电子邮件发送pdf文件时,我可以在mac上查看该文件。。但同样的文件通过android dropbox api上传到dropbox,当我在mac上打开时,pdf文件似乎是空的。但dropbox版本和电子邮件版本的文件大小相同。这种情况只发生在pdf文件上,当我通过dropbox android api发送文本文件时,效果很好 以前有人经历过这样的问题吗?有什么解决办法吗?我正在使用dropbox开发者端最新的

我写了一个android应用程序,它通过电子邮件和dropbox导出pdf文件。当我使用电子邮件发送pdf文件时,我可以在mac上查看该文件。。但同样的文件通过android dropbox api上传到dropbox,当我在mac上打开时,pdf文件似乎是空的。但dropbox版本和电子邮件版本的文件大小相同。这种情况只发生在pdf文件上,当我通过dropbox android api发送文本文件时,效果很好

以前有人经历过这样的问题吗?有什么解决办法吗?我正在使用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的内容之前就开始编程了。dropbox似乎有两种类型的API。一个用于使用syncpi的正常文本写入,另一个用于更复杂的任务,如使用coreapi上传文件


至于为什么你的代码不起作用,我认为你上面的评论是正确的,因为它涉及到试图像读取文本一样读取文件。(至少,您可能在文件中写入了几行新行。)

但无论如何,真的不需要那个代码。不要调用您编写的
writeDropBoxFileFromFile
方法,只需执行
dropboxFile.writeFromExistingFile(localFile,false)


(文档在这里:。只需查找“
writeFromExistingFile
”)

是否使用特定于文本文件的API函数?@mkl im根据sdk提供的示例使用文件同步API支持。。我不知道任何基于文件类型的特殊api。根据我错过的文件类型,dropbox是否有特殊的API?@mkl你说得对。。我使用了错误的api。。我应该使用核心api,而不是pdf或图像文件的同步api。。谢谢,伙计……这绝对是错的。这两种API对于文本文件和非文本文件都同样有效。例如,同步API附带了一个ImageGrid示例,显示如何使用图像文件。如果您仍对使用同步API感兴趣,并想找出代码中的错误,请共享代码,以便我们可以尝试发现错误。@smarx这是我第二次尝试使用此API。。我不知道它是怎么工作的。。我在上面添加了代码。但我想现在我知道哪里出了问题。。我想我在写入文件时使用了stringer builder。。。这对无文本文件不起作用。。提前感谢:)