0

我想在 android 内部存储(或 sdcard)中同步一个 txt 文件,并让它自动同步到我的保管箱中的 txt。我使用了 dropbox api,但不知道如何实现这一点。请帮助。

更新:

这是我现在将 txt 内容获取到 LogCat 的函数:

public void getFromDbx(){

        try {
            mDbxAcctMgr.startLink((Activity)this, REQUEST_LINK_TO_DBX);

            final String TEST_DATA = " Database does not exist yet";

            final String TEST_FILE_NAME = "data.txt";
            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.

            //dont need this code -- ahmed

            // 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();
                }

            }

            // 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();
                }

                Log.i("dbx",resultData);
            } else if (dbxFs.isFolder(testPath)) {

            }
        } catch (IOException e) {

        }

我的创建:

super.onCreate(savedInstanceState);
        setContentView(R.layout.main_screen);

        //connect dbx to account
                mDbxAcctMgr = DbxAccountManager.getInstance(getApplicationContext(), appKey, appSecret);

                //get data from ems_data.txt from dropbox account
                getFromDbx();

我的 OnActivityResult:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_LINK_TO_DBX) {
            if (resultCode == Activity.RESULT_OK) {
                getFromDbx();
            } else {
                Log.i("dbxems", "Link to Dropbox failed or was cancelled.");
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testdatabaseactivity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.CALL_PHONE" >

    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:debuggable="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testdatabaseactivity.TestDatabaseActivity"
            android:label="@string/app_name" >
            <intent-filter>
            </intent-filter>
        </activity>

        <instrumentation
            android:name="android.test.InstrumentationTestRunner"
            android:label="your tests label"
            android:targetPackage="com.example.testdatabaseactivity" />

        <uses-library android:name="android.test.runner" />

        <activity
            android:name="com.example.testdatabaseactivity.MainScreenActivity"
            android:label="@string/title_activity_main_screen" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.testdatabaseactivity.SearchByName"
            android:label="@string/title_activity_search_by_name"
            android:windowSoftInputMode="stateHidden" >
        </activity>

        <activity android:name="com.dropbox.sync.android.DbxAuthActivity" />
        <activity
            android:name="com.dropbox.client2.android.AuthActivity"
            android:launchMode="singleTask" >
            <intent-filter>
                <data android:scheme="db-KEY_HERE" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <service
            android:name="com.dropbox.sync.android.DbxSyncService"
            android:enabled="true"
            android:exported="false"
            android:label="Dropbox Sync" />
    </application>

</manifest>

编辑:没有错误,应用程序显示身份验证屏幕,我单击允许,然后没有任何反应。我可以按允许 10 次,但什么也没有发生..

4

1 回答 1

2

getFromDbx中,您调用startLink,这会打开 UI 以链接帐户。当成功时,你的onActivityResult被执行,在你调用的地方getFromDbx,它startLink再次调用。:-) 对我来说看起来像是一个无限循环。

于 2013-09-01T00:59:23.247 回答