Dropbox无法在Android中上传文本文件

时间:2015-06-09 05:38:24

标签: android eclipse dropbox dropbox-api

下面给出了代码,我上传文本文件时遇到问题 我能做什么 - 身份验证工作正常 -Entry response = null;

   try {
                response = mDBApi.putFile("/file1234.txt", inputStream,
                        file.length(), null, null);
            } catch (DropboxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.i("DbExampleLog", "The uploaded file's rev is: " + response.rev);

在此部分中,响应对象

中的错误为NullPointerException
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.DropboxAPI.Entry;
import com.dropbox.client2.android.AndroidAuthSession;
import com.dropbox.client2.exception.DropboxException;
import com.dropbox.client2.session.AppKeyPair;

public class MainActivity extends ActionBarActivity {
    final static private String APP_KEY = "xxxxxxxxxxx";
    final static private String APP_SECRET = "xxxxxxxxxxx";

// In the class declaration section:
private DropboxAPI<AndroidAuthSession> mDBApi;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initDropBox();

}

public void initDropBox() {
    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
    AndroidAuthSession session = new AndroidAuthSession(appKeys);
    mDBApi = new DropboxAPI<AndroidAuthSession>(session);
    mDBApi.getSession().startOAuth2Authentication(MainActivity.this);
    uploadFile();
}

public void uploadFile() {
    File file = new File("/storage/emulated/0/file.txt");
    FileInputStream inputStream = null;
    try {
        inputStream = new FileInputStream(file);
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    Entry response = null;
    try {
        response = mDBApi.putFile("/file1234.txt", inputStream,
                file.length(), null, null);
    } catch (DropboxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Log.i("DbExampleLog", "The uploaded file's rev is: " + response.rev);
}

public void click(View v) {
    switch (v.getId()) {
    case R.id.button1: {
        // MyActivity below should be your activity class name
        dropBoxPreference = getSharedPreferences("dropBoxPref",
                MODE_MULTI_PROCESS);
        // if (dropBoxPreference.getString("accessToken", null) == null) {

        //uploadFile();
        // }
        break;
    }
    case R.id.button2: {

    }
    }
}

SharedPreferences dropBoxPreference;
Editor editor;

@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.
             */
            // dropBoxPreference = getSharedPreferences("dropBoxPref",
            // MODE_MULTI_PROCESS);
            // editor = dropBoxPreference.edit();
            // String checkToken =
            // dropBoxPreference.getString("accessToken",
            // null);
            // if (checkToken == null) {
            // editor.putString("accessToken", accessToken);
            // editor.commit();

            // }
        } catch (IllegalStateException e) {
            Log.i("DbAuthLog", "Error authenticating", e);
        }
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

任何帮助感谢...提前致谢。

1 个答案:

答案 0 :(得分:0)

以下是我自己提出的上述问题的解决方案。

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

- &gt;在子线程中调用uploadFile()和downLoadFile()方法,否则会给你异常 - &gt;为此使用AsyncTask并在doInBackground方法中调用上述方法。

希望,这对很多人都有帮助。谢谢