我现在使用 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 了解如何实现此目的。