在Android中使用Dropbox API创建文件夹

用户名

有谁知道如何使用用于Android的Dropbox api以编程方式创建文件夹?我未使用同步api。我设法上传了图片和文件,但无法创建文件夹。

这是我的上载asynctask:

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();
    }



}

请帮忙。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用API在OneDrive上创建文件夹

使用 PHP 创建文件夹 Google Drive API

Dropbox API-创建文件夹时添加文件夹概述

在Android中创建文件夹

使用Swift创建文件夹

使用WshUserEnv创建文件夹

如何使用PHP在Dropbox上创建文件夹

SharePoint在Powershell中使用Graph API创建文件夹

如何在Flutter应用中使用Google Drive API创建文件夹?

在Powershell中使用先前的日期创建文件夹

使用 OneDrive API 在另一个文件夹中创建文件夹

Google Drive API 使用 curl 调用创建文件而不是文件夹

使用API在Amazon S3存储桶中创建文件夹/上传文件

使用用户输入创建文件夹:Android

使用javascript在android中创建文件夹

Android Studio在Dropbox文件夹(\ Dropbox \ .AndroidStudioPreview)中创建文件

使用python创建用于创建文件夹的循环

如何在Google Drive Android API中获取已创建文件夹的DriveId

使用Google Drive API创建文件夹:资源没有“插入”方法

如何使用Node.js API在DigitalOcean存储桶中创建文件夹?

使用EWS API在其他邮箱中创建文件夹

如何使用Google的Python Drive Client API在Team Drive中创建文件夹?

如何使用Google API在Team Drive中创建文件夹?

Microsoft onedrive:使用API密钥创建文件夹而无需登录

OneDrive FB 使用 Microsoft Graph API Powershell 创建文件夹

使用Servlet创建文件夹并上传文件

R:使用文件名创建文件夹

无法创建文件夹“调试” Android Studio

无法在Android /数据中创建文件夹