Using Dropbox API to upload a file with Android

前端 未结 5 516
庸人自扰
庸人自扰 2020-12-08 15:31

How can I upload a File (graphic, audio and video file) with Android using the Dropbox API to Dropbox? I followed the tutorial on the Dropbox SDK Android page and could get

5条回答
  •  忘掉有多难
    2020-12-08 16:26

    Here is another implementation of Dropbox API to upload and download a file. This can be implemented for any type of file.

    String file_name = "/my_file.txt";
    String file_path = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + file_name;
    AndroidAuthSession session;
    
    public void initDropBox() {
    
        AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
        session = new AndroidAuthSession(appKeys);
        mDBApi = new DropboxAPI(session);
        mDBApi.getSession().startOAuth2Authentication(MyActivity.this);
    
    }
    
    Entry response;
    
    public void uploadFile() {
        writeFileContent(file_path);
        File file = new File(file_path);
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    
    
        try {
            response = mDBApi.putFile("/my_file.txt", inputStream,
                    file.length(), null, null);
            Log.i("DbExampleLog", "The uploaded file's rev is: " + response.rev);
        } catch (DropboxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    
        }
    
    }
    public void downloadFile() {
    
        File file = new File(file_path);
        FileOutputStream outputStream = null;
    
        try {
            outputStream = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        DropboxFileInfo info = null;
        try {
            info = mDBApi.getFile("/my_file.txt", null, outputStream, null);
    
    
    
            Log.i("DbExampleLog", "The file's rev is: "
                    + info.getMetadata().rev);
        } catch (DropboxException e) {
            // TODO Auto-generated catch block
    
            e.printStackTrace();
        }
    
    }
    
    @Override
        protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();
            if (mDBApi.getSession().authenticationSuccessful()) {
                try {
                    // Required to complete auth, sets the access token on the
                    // session
    
                mDBApi.getSession().finishAuthentication();
    
                String accessToken = mDBApi.getSession().getOAuth2AccessToken();
    
                /**
                 * You'll need this token again after your app closes, so it's
                 * important to save it for future access (though it's not shown
                 * here). If you don't, the user will have to re-authenticate
                 * every time they use your app. A common way to implement
                 * storing keys is through Android's SharedPreferences API.
                 */
    
            } catch (IllegalStateException e) {
                Log.i("DbAuthLog", "Error authenticating", e);
            }
        }
    }
    

    ->Call uploadFile() and downLoadFile() method in child thread otherwise it will give you exception

    ->For that use AsyncTask and call these above method in doInBackground method.

    Hope this will be helpful...Thanks

提交回复
热议问题