繁体   English   中英

如何在android中实现Dropbox?我已经成功实现了API,但仍然无法正常工作

[英]How to Implement Dropbox in android ?I have implemented the API successfully but it still not working

如果有人拥有示例程序,请在此处提供在我们的项目中实施的必要条件。

我是Android技术的新手,我遇到了问题。如果有人需要代码,那么我将在这里提供。

提前致谢。

尝试这个,

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<AndroidAuthSession>(session);
    mDBApi.getSession().startOAuth2Authentication(ChatActivity.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);
        }
    }
}

->在子线程中调用uploadFile(...)downLoadFile(...)方法,否则会给您异常->为此,请使用AsyncTask并在doInBackground(...)方法中调用上述方法

希望,这将有所帮助。谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM