[英]Android Dropbox Core Api upload file sample
Does anyone have a working Android Dropbox Core Api upload file sample to share? 有没有人可以分享有效的Android Dropbox Core Api上传文件示例? Their examples are using Eclipse while the latest Android Studio is based on IntelliJ and I had very little success running them.
他们的示例使用的是Eclipse,而最新的Android Studio是基于IntelliJ的,运行它们的成功很少。 Please share a code snippet of the upload!
请分享上传的代码段! Thanks!
谢谢!
Going to answer my own question. 要回答我自己的问题。 I got this working;
我得到了这个工作; Read carefully the example on their website https://www.dropbox.com/developers/core/start/android ;
仔细阅读其网站https://www.dropbox.com/developers/core/start/android上的示例;
Insure that in your AndoidManifest you have the following section 确保在您的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>
make sure that in the above manifest your own API key is added with the "db-" prefix; 确保在上面的清单中,您自己的API密钥添加了“ db-”前缀;
if you want the upload example to work replace the mDBApi.putFile... with 如果您希望上载示例正常运行,请用以下命令替换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();}
Check your Manifest file and add the following code in the application tag. 检查清单文件,然后在application标记中添加以下代码。
<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>
don't forget to change the value of data tag.( "db-" + your app key). 不要忘记更改数据标签的值。( “ db-” +您的应用程序密钥)。
if you want the upload example just do the steps below : 如果要上传示例,请执行以下步骤:
1) add a fab button into your activity. 1)在您的活动中添加一个fab按钮。
2) into the onClickListener() methode, call this methode : 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) override activityResult to get the data from file picker like this 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) implement upload methode like the code bellow 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) create a java class name it to UploadFileTask.java and put the bellow code into it: 5)创建一个将其命名为UploadFileTask.java的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; } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.