【问题标题】:How to sync image folder from Android phone to Dropbox using Dropbox Sync Api如何使用 Dropbox Sync Api 将图像文件夹从 Android 手机同步到 Dropbox
【发布时间】:2014-03-04 12:40:18
【问题描述】:

如何使用 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);
    }
}

【问题讨论】:

    标签: android dropbox-api


    【解决方案1】:

    您的代码尝试写入名为Pars 的文件,但您只有写入图像文件的权限(扩展名为.jpg.png 等的文件)。如果您将文件名更改为带有图像扩展名的名称,例如Pars.jpg,您将能够编写它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多