如何使用Dropbox Sync Api将图像文件夹从Android手机同步到Dropbox

时间:2014-03-04 12:40:18

标签: android dropbox-api

如何使用Dropbox Sync Api将Android手机中的整个图像文件夹同步到Dropbox?

我的应用只能访问图像文件类型,因为我只想将图像同步到Dropbox。当我尝试实现当前代码时出现此错误:

03-04 20:38:42.010: W/libDropboxSync.so(thr)(23160): util.cpp:124: int     dropbox_wait_for_first_sync(dbx_client_t*) should not be called on the main thread
03-04 20:38:42.020: W/libDropboxSync.so(ERR)(23160): DROPBOX_DISALLOWED: sync.hpp:300: app     is not allowed to create file p(/i9)

代码的主要部分:

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dropbox_activity);
    mTestOutput = (TextView) findViewById(R.id.test_output);
    mLinkButton = (Button) findViewById(R.id.link_button);
    mLinkButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onClickLinkToDropbox();
        }
    });

    mDbxAcctMgr = DbxAccountManager.getInstance(getApplicationContext(), appKey, appSecret);
}

@Override
protected void onResume() {
    super.onResume();
    if (mDbxAcctMgr.hasLinkedAccount()) {
        showLinkedView();
        doDropboxTest();
    } else {
        showUnlinkedView();
    }
}

private void showLinkedView() {
    mLinkButton.setVisibility(View.GONE);
    mTestOutput.setVisibility(View.VISIBLE);
}

private void showUnlinkedView() {
    mLinkButton.setVisibility(View.VISIBLE);
    mTestOutput.setVisibility(View.GONE);
}

private void onClickLinkToDropbox() {
    mDbxAcctMgr.startLink((Activity)this, REQUEST_LINK_TO_DBX);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_LINK_TO_DBX) {
        if (resultCode == Activity.RESULT_OK) {
            doDropboxTest();
        } else {
            mTestOutput.setText("Link to Dropbox failed or was cancelled.");
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

private void doDropboxTest() {
    mTestOutput.setText("Dropbox Sync API Version "+DbxAccountManager.SDK_VERSION_NAME+"\n");
    try {
        final String TEST_DATA = "Hello Dropbox";
        final String TEST_FILE_NAME = "Pars";

        DbxPath testPath = new DbxPath(DbxPath.ROOT, TEST_FILE_NAME);

        // Create DbxFileSystem for synchronized file access.
        DbxFileSystem dbxFs = DbxFileSystem.forAccount(mDbxAcctMgr.getLinkedAccount());

        // Print the contents of the root folder.  This will block until we can
        // sync metadata the first time.
        List<DbxFileInfo> infos = dbxFs.listFolder(DbxPath.ROOT);
        mTestOutput.append("\nContents of app folder:\n");
        for (DbxFileInfo info : infos) {
            mTestOutput.append("    " + info.path + ", " + info.modifiedTime + '\n');
        }

        // Create a test file only if it doesn't already exist.
        if (!dbxFs.exists(testPath)) {
            DbxFile testFile = dbxFs.create(testPath);
            try {
                testFile.writeString(TEST_DATA);
            } finally {
                testFile.close();
            }
            mTestOutput.append("\nCreated new file '" + testPath + "'.\n");
        }

        // Read and print the contents of test file.  Since we're not making
        // any attempt to wait for the latest version, this may print an
        // older cached version.  Use getSyncStatus() and/or a listener to
        // check for a new version.
        if (dbxFs.isFile(testPath)) {
            String resultData;
            DbxFile testFile = dbxFs.open(testPath);
            try {
                resultData = testFile.readString();
            } finally {
                testFile.close();
            }
            mTestOutput.append("\nRead file '" + testPath + "' and got data:\n    " + resultData);
        } else if (dbxFs.isFolder(testPath)) {
            mTestOutput.append("'" + testPath.toString() + "' is a folder.\n");
        }
    } catch (IOException e) {
        mTestOutput.setText("Dropbox test failed: " + e);
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码尝试编写名为Pars的文件,但您只有编写图像文件的权限(具有.jpg.png等扩展名的文件)。如果您将文件名更改为具有图片扩展名的内容,例如Pars.jpg,则可以将其编写。

相关问题