是否有人有可用的Android Dropbox Core Api上传文件样本? 他们的例子是使用Eclipse,而最新的Android Studio是基于IntelliJ的,我在运行它们方面取得的成功很少。 请分享上传的代码段! 谢谢!
答案 0 :(得分:0)
回答我自己的问题。我得到了这个工作;仔细阅读他们网站https://www.dropbox.com/developers/core/start/android上的示例;
确保您的AndoidManifest中有以下部分
<activity
android:name="com.dropbox.client2.android.AuthActivity"
android:launchMode="singleTask"
android:configChanges="orientation|keyboard">
<intent-filter>
<!-- Change this to be db- followed by your app key -->
<data android:scheme="db-your 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>
确保在上面的清单中添加了您自己的API密钥,其中包含&#34; db - &#34; 前缀;
如果您希望上传示例有效,请用
替换mDBApi.putFile ... if ( mDBApi.getSession().authenticationSuccessful())
{
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
File file = new File("/somefile.txt");
FileInputStream inputStream = new FileInputStream(file);
DropboxAPI.Entry response = mDBApi.putFile("/somefile.txt", inputStream, file.length(), null, null);
Log.i("DbExampleLog", "The uploaded file's rev is: " + response.rev);
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();}
答案 1 :(得分:0)
检查清单文件,然后在应用程序标记中添加以下代码。
<activity
android:name="com.dropbox.client2.android.AuthActivity"
android:launchMode="singleTask"
android:configChanges="orientation|keyboard">
<intent-filter>
<!-- Change this to be db-followed by your app key-->
<data android:scheme="db-your 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>
不要忘记更改 data 标签的值。(“ db-” +您的应用程序密钥)。
如果要上传示例,请执行以下步骤:
1)将fab按钮添加到您的活动中。
2)进入onClickListener()方法,将此方法称为:
private void launchFilePicker() {
// Launch intent to pick file for upload
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, PICKFILE_REQUEST_CODE);}
3)覆盖activityResult以这样从文件选择器获取数据
@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICKFILE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// This is the result of a call to launchFilePicker
uploadFile(data.getData().toString());
}
}
}
4)实现上载方法,例如下面的代码
private void uploadFile(String fileUri) {
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setCancelable(false);
dialog.setMessage("Uploading");
dialog.show();
new UploadFileTask(this, DropboxClientFactory.getClient(), new UploadFileTask.Callback() {
@Override
public void onUploadComplete(FileMetadata result) {
dialog.dismiss();
String message = result.getName() + " size " + result.getSize() + " modified " +
DateFormat.getDateTimeInstance().format(result.getClientModified());
Toast.makeText(FilesActivity.this, message, Toast.LENGTH_SHORT)
.show();
// Reload the folder
loadData();
}
@Override
public void onError(Exception e) {
dialog.dismiss();
Log.e(TAG, "Failed to upload file.", e);
Toast.makeText(FilesActivity.this,
"An error has occurred",
Toast.LENGTH_SHORT)
.show();
}
}).execute(fileUri, mPath);
}
5)创建一个Java类,将其命名为 UploadFileTask.java 并将下面的代码放入其中:
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import com.dropbox.core.DbxException;
import com.dropbox.core.v2.DbxClientV2;
import com.dropbox.core.v2.files.FileMetadata;
import com.dropbox.core.v2.files.WriteMode;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Async task to upload a file to a directory
*/
class UploadFileTask extends AsyncTask<String, Void, FileMetadata> {
private final Context mContext;
private final DbxClientV2 mDbxClient;
private final Callback mCallback;
private Exception mException;
public interface Callback {
void onUploadComplete(FileMetadata result);
void onError(Exception e);
}
UploadFileTask(Context context, DbxClientV2 dbxClient, Callback callback) {
mContext = context;
mDbxClient = dbxClient;
mCallback = callback;
}
@Override
protected void onPostExecute(FileMetadata result) {
super.onPostExecute(result);
if (mException != null) {
mCallback.onError(mException);
} else if (result == null) {
mCallback.onError(null);
} else {
mCallback.onUploadComplete(result);
}
}
@Override
protected FileMetadata doInBackground(String... params) {
String localUri = params[0];
File localFile = UriHelpers.getFileForUri(mContext, Uri.parse(localUri));
if (localFile != null) {
String remoteFolderPath = params[1];
// Note - this is not ensuring the name is a valid dropbox file name
String remoteFileName = localFile.getName();
try (InputStream inputStream = new FileInputStream(localFile)) {
return mDbxClient.files().uploadBuilder(remoteFolderPath + "/" + remoteFileName)
.withMode(WriteMode.OVERWRITE)
.uploadAndFinish(inputStream);
} catch (DbxException | IOException e) {
mException = e;
}
}
return null;
}
}