【问题标题】:access dropbox files from my app, download and upload files to dropbox从我的应用程序访问保管箱文件,下载文件并将其上传到保管箱
【发布时间】:2013-12-03 02:50:20
【问题描述】:

在我的应用程序中,我需要用户访问 Dropbox,查看文件(所有文件夹),能够下载和上传图片和小型音频文件等文件。我已经尝试使用 DBRolette 来理解这个概念。让它也能工作。身份验证也很好。但我不知道如何执行下载/上传/查看所有文件等任务。

我也读到了“选择器”和“保护器”,但我认为我应该使用“核心 Api”。请指导我。

任何让我再次行动的帮助将不胜感激。

谢谢

【问题讨论】:

标签: android dropbox dropbox-api


【解决方案1】:

我现在使用 Dropbox 已经很长时间了。我不需要查看目录,但我正在访问多个区域的文件。如果您的核心 Dropbox 已经在工作,那么您可以写入任何文件夹,如果没有,将使用此文件夹创建。

// located in your dropboxHelper class
    public void writeToDropbox(File src) {
        String directory1 = "/TopFolder/";
        // Or for a sub folder
        String orThisDirectory = "/TopFolder/SubFolder/";
        myAsyncTask backup = new myAsyncTask(ctx, mDBApi, directory1, src);
        backup.execute();
    }

文件名将与您发送的文件相同。如果您需要 Asynctask 代码,我也可以包含它。让我知道。 为了拉取文件,我使用了这个基本代码。

public void restore(String fileGet, File fileSave, String sPath,
        String mFolder) {
    File file = new File(fileGet);
    try {
        FileOutputStream outputStream = new FileOutputStream(fileSave);
        otherAsyncTask restore = new otherAsyncTask (ctx, mDBApi,outputStream,
                file, sPath, mFolder);
        restore.execute();

    }

这将包括上传和下载。我只使用核心 API。如果您还需要 AsyncTask 代码,请询问。我想你可能有那个部分。

更新 获取 DropboxHelper 的实例。将其放在您发送信息的班级中。

// Dropbox setup
    if (dropbox_option) {
        mDropbox = new DropboxHelper(ctx);
        if (!mDropbox.isLinked()) {
            mDropbox.completeAuthenticationDropbox();
        }
    }

在 Dropbox 帮助类中,我拥有所有的连接代码和读写方法。

public static DropboxHelper getInstance(Context context) {
    if (mHelper == null) {
        mHelper = new DropboxHelper(context);
    }
    return mHelper;
}

public static DropboxHelper getInstance() throws Exception {
    if (mHelper == null)
        throw new Exception("DropboxHelper not yet instantiated");

    return mHelper;
}

public DropboxHelper(Context context) {
    super();
    ctx = context;

    // create session
    AndroidAuthSession session = buildSession();
    mDBApi = new DropboxAPI<AndroidAuthSession>(session);

    if (isLinked()) {
        AccessKeys = getKeys();
        AccessTokenPair access = new AccessTokenPair(AccessKeys[0],
                AccessKeys[1]);
        mDBApi.getSession().setAccessTokenPair(access);
    } else {
        mDBApi.getSession().startAuthentication(ctx);
    }
}

private String[] getKeys() {
    prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    String key = prefs.getString(Global.DROPBOX_ACCESS_KEY_NAME, null);
    String secret = prefs
            .getString(Global.DROPBOX_ACCESS_SECRET_NAME, null);
    if (key != null && secret != null) {
        String[] ret = new String[2];
        ret[0] = key;
        ret[1] = secret;
        return ret;
    } else {
        return null;
    }
}


 // Clear token 

public void clearKeysToken() {
    prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    Editor edit = prefs.edit();
    edit.clear();
    edit.commit();
}

protected void storeKeys(String key, String secret) {
    // prefs = this.getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
    prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    Editor edit = prefs.edit();
    edit.putString(Global.DROPBOX_ACCESS_KEY_NAME, key);
    edit.putString(Global.DROPBOX_ACCESS_SECRET_NAME, secret);
    edit.commit();
}
       // Create a session

private AndroidAuthSession buildSession() {
    AppKeyPair appKeyPair = new AppKeyPair(Global.APP_KEY,
            Global.APP_SECRET);
    AndroidAuthSession session;
    String[] stored = getKeys();
    if (stored != null) {
        AccessTokenPair accessToken = new AccessTokenPair(stored[0],
                stored[1]);
        session = new AndroidAuthSession(appKeyPair,
                Global.DROPBOX_ACCESS_TYPE, accessToken);
    } else {
        session = new AndroidAuthSession(appKeyPair,
                Global.DROPBOX_ACCESS_TYPE);
    }

    return session;
}

// Complete authentication

public void completeAuthenticationDropbox() {
    session = mDBApi.getSession();
    // The next part must be inserted in the onResume() method of the
    // activity from which session.startAuthentication() was called, so
    // that Dropbox authentication completes properly.
    if (session.authenticationSuccessful()) {
        try {
            // complete authentication
            session.finishAuthentication();
            // save login credentials
            TokenPair tokens = session.getAccessTokenPair();
            storeKeys(tokens.key, tokens.secret);
            // setLoggedIn(true);
        } catch (IllegalStateException e) {
            Toast.makeText(ctx,
                    "Couldn't authenticate with Dropbox:" + e.getMessage(),
                    Toast.LENGTH_LONG).show();
            Log.i(TAG, "Error authenticating", e);
        } finally {
            if (isLinked()) {
                Log.d(TAG, "dropbox is linked");
            } else {
                Log.d(TAG, "Dropbox is NOT linked.");
            }
        }
    } else {
        Log.d(TAG, "WTF");
    }
}

// Returns true if you are connected to Dropbox
public boolean isLinked() {
    return mDBApi.getSession().isLinked();

}
public void logIn() {
    Log.i(TAG, "Login");
    mDBApi.getSession().startAuthentication(ctx);
}

通过筛选 this code 了解如何实现此目的。

【讨论】:

  • 非常感谢。我对保管箱活动的了解仅限于示例应用 DBRoulette。这个应用程序的作用是访问保管箱并打开一个随机 jpeg 文件。另一方面,我需要查看文件、上传和下载到 Dropbox。你能进一步指导我吗?例如如何实现您刚刚提供的方法。不,我不需要同步部分
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-08
  • 1970-01-01
  • 2013-07-02
  • 2012-12-25
  • 2013-01-01
  • 2011-10-21
  • 2018-08-05
相关资源
最近更新 更多