使用android dropbox api在中创建文件夹
有人知道如何使用dropbox api为android编程创建文件夹吗?我没有使用同步api。我已成功上载图像和文件,但无法创建文件夹 这是我的上传任务:使用android dropbox api在中创建文件夹,android,dropbox-api,Android,Dropbox Api,有人知道如何使用dropbox api为android编程创建文件夹吗?我没有使用同步api。我已成功上载图像和文件,但无法创建文件夹 这是我的上传任务: public class UploadFile extends AsyncTask<Void, Long, Boolean> { private DropboxAPI<?> mApi; private String mPath; private File mFile; private
public class UploadFile extends AsyncTask<Void, Long, Boolean> {
private DropboxAPI<?> mApi;
private String mPath;
private File mFile;
private long mFileLen;
private UploadRequest mRequest;
private Context mContext;
private ProgressDialog mDialog;
final static private String ACCOUNT_PREFS_NAME = "prefs";
private String mErrorMsg;
public UploadFile(Context context, DropboxAPI<?> api, String dropboxPath,
File file) {
mContext = context;
mFileLen = file.length();
mApi = api;
mPath = dropboxPath;
mFile = file;
mDialog = new ProgressDialog(context);
mDialog.setMax(100);
mDialog.setMessage("Uploading " + file.getName());
mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mDialog.setProgress(0);
mDialog.setButton("Cancel", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// This will cancel the putFile operation
mRequest.abort();
}
});
mDialog.show();
}
@Override
protected Boolean doInBackground(Void... params) {
try {
// By creating a request, we get a handle to the putFile operation,
// so we can cancel it later if we want to
FileInputStream fis = new FileInputStream(mFile);
String path = mPath + mFile.getName();
mRequest = mApi.putFileOverwriteRequest(path, fis, mFile.length(),
new ProgressListener() {
@Override
public long progressInterval() {
// Update the progress bar every half-second or so
return 500;
}
@Override
public void onProgress(long bytes, long total) {
publishProgress(bytes);
}
});
if (mRequest != null) {
mRequest.upload();
return true;
}
} catch (DropboxUnlinkedException e) {
// This session wasn't authenticated properly or user unlinked
mErrorMsg = "This app wasn't authenticated properly.";
} catch (DropboxFileSizeException e) {
// File size too big to upload via the API
mErrorMsg = "This file is too big to upload";
} catch (DropboxPartialFileException e) {
// We canceled the operation
mErrorMsg = "Upload canceled";
} catch (DropboxServerException e) {
// Server-side exception. These are examples of what could happen,
// but we don't do anything special with them here.
if (e.error == DropboxServerException._401_UNAUTHORIZED) {
// Unauthorized, so we should unlink them. You may want to
// automatically log the user out in this case.
} else if (e.error == DropboxServerException._403_FORBIDDEN) {
// Not allowed to access this
} else if (e.error == DropboxServerException._404_NOT_FOUND) {
// path not found (or if it was the thumbnail, can't be
// thumbnailed)
} else if (e.error == DropboxServerException._507_INSUFFICIENT_STORAGE) {
// user is over quota
} else {
// Something else
}
// This gets the Dropbox error, translated into the user's language
mErrorMsg = e.body.userError;
if (mErrorMsg == null) {
mErrorMsg = e.body.error;
}
} catch (DropboxIOException e) {
e.printStackTrace();
// Happens all the time, probably want to retry automatically.
mErrorMsg = "Network error. Try again.";
} catch (DropboxParseException e) {
// Probably due to Dropbox server restarting, should retry
mErrorMsg = "Dropbox error. Try again.";
} catch (DropboxException e) {
// Unknown error
mErrorMsg = "Unknown error. Try again.";
} catch (FileNotFoundException e) {
}
return false;
}
@Override
protected void onProgressUpdate(Long... progress) {
int percent = (int) (100.0 * (double) progress[0] / mFileLen + 0.5);
mDialog.setProgress(percent);
}
@Override
protected void onPostExecute(Boolean result) {
mDialog.dismiss();
if (result) {
showToast("Successfully uploaded");
// mApi.getSession().unlink();
//
// // Clear our stored keys
// clearKeys();
} else {
showToast(mErrorMsg);
}
}
private void showToast(String msg) {
Toast error = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
error.show();
}
private void clearKeys() {
SharedPreferences prefs = mContext.getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
Editor edit = prefs.edit();
edit.clear();
edit.commit();
}
}
公共类上载文件扩展异步任务{
私有DropboxAPI mApi;
私有字符串mPath;
私有文件文件;
私人长姆费伦;
私有上传请求mRequest;
私有上下文;
私人住宅;
最终静态私有字符串帐户\u PREFS\u NAME=“PREFS”;
私有字符串merrormg;
公共上载文件(上下文上下文、DropboxAPI、字符串dropboxPath、,
(文件){
mContext=上下文;
mfilen=file.length();
mApi=api;
mPath=dropboxPath;
mFile=文件;
mDialog=新建进度对话框(上下文);
mDialog.setMax(100);
mDialog.setMessage(“上载”+文件.getName());
mDialog.setProgressStyle(ProgressDialog.STYLE_水平);
mDialog.setProgress(0);
mDialog.setButton(“取消”,新建OnClickListener()){
public void onClick(DialogInterface dialog,int which){
//这将取消putFile操作
mRequest.abort();
}
});
mDialog.show();
}
@凌驾
受保护的布尔doInBackground(Void…params){
试一试{
//通过创建请求,我们获得putFile操作的句柄,
//如果我们想的话,我们可以以后取消
FileInputStream fis=新的FileInputStream(mFile);
字符串路径=mPath+mFile.getName();
mRequest=mApi.putFileOverwriteRequest(路径,fis,mFile.length(),
新ProgressListener(){
@凌驾
公共长时间间隔(){
//大约每半秒更新一次进度条
返回500;
}
@凌驾
public void onProgress(长字节,长总计){
出版进度(字节);
}
});
if(mRequest!=null){
mRequest.upload();
返回true;
}
}捕获(DropboxUnlinkedException){
//此会话未正确验证或用户未链接
mErrorMsg=“此应用未正确验证。”;
}捕获(DropboxFileSizeException e){
//文件太大,无法通过API上载
mErrorMsg=“此文件太大,无法上载”;
}捕获(DropboxPartialFileE异常){
//我们取消了手术
mErrorMsg=“上传已取消”;
}捕获(DropboxServerException e){
//服务器端异常。这些是可能发生的情况的示例,
//但我们在这里对他们没有什么特别的处理。
如果(e.error==DropboxServerException.\u 401\u未经授权){
//未经授权,所以我们应该取消它们的链接。您可能想要
//在这种情况下,自动注销用户。
}否则如果(e.error==DropboxServerException.\u 403\u禁止){
//不允许访问此文件
}else if(e.error==DropboxServerException.\u 404\u未找到){
//未找到路径(或者如果是缩略图,则无法
//拇指钉)
}否则如果(e.error==DropboxServerException.\u 507\u存储不足){
//用户超过配额
}否则{
//别的
}
//这将获取Dropbox错误,并将其翻译成用户的语言
mErrorMsg=e.body.userError;
if(mErrorMsg==null){
mErrorMsg=e.body.error;
}
}捕获(DropboxIOE异常){
e、 printStackTrace();
//经常发生,可能要自动重试。
mErrorMsg=“网络错误。请重试。”;
}捕获(DropboxParse异常){
//可能是由于Dropbox服务器重新启动,应该重试
mErrorMsg=“Dropbox错误。请重试。”;
}捕获(DropBoxexException e){
//未知错误
mErrorMsg=“未知错误。请重试。”;
}catch(filenotfounde异常){
}
返回false;
}
@凌驾
受保护的void onProgressUpdate(长…进度){
整数百分比=(整数)(100.0*(双倍)进度[0]/mFileLen+0.5);
mDialog.setProgress(百分比);
}
@凌驾
受保护的void onPostExecute(布尔结果){
mDialog.discouse();
如果(结果){
showToast(“已成功上载”);
//mApi.getSession().unlink();
//
////清除存储的密钥
//clearKeys();
}否则{
showtoots(mErrorMsg);
}
}
私有void showtoos(字符串msg){
Toast error=Toast.makeText(mContext、msg、Toast.LENGTH\u LONG);
error.show();
}
私有void clearKeys(){
SharedReferences prefs=mContext.getSharedReferences(帐户名称,0);
Editor edit=prefs.edit();
edit.clear();
edit.commit();
}
}
请提供帮助。使用以下命令进行DIR:
“/your\u folder\u name/”您需要
DropboxAPI
的createFolder
方法。请参阅。创建文件夹时,您可以简单地给出这样的答案!这很简单
dropbox.putFile("Mynumber2/myregion2/"+"/"+"A"+"/"+"B"+"/"+"ENTRY.db", fileInputStream,file.length(), null, null);
如果您这样做,mynumber2-->myregion2-->A-->B将是您在dropbox上创建的文件夹结构 根据dropbox文档
try {
// creating folder
val fo = client.files().createFolderV2(File.separator + folderPath)
} catch (ex: CreateFolderErrorException) {
if (ex.errorValue.isPath && ex.errorValue.pathValue.isConflict) {
// folder already exist
}
}
如何调用要上载的方法以及“dropboxPath”的值是多少?UploadFile upload=新建UploadFile(DropboxDownload.this,mApi,DIR,filefordropbox);目录为“/”,FILEFORDROPBOX