Dropbox issues not able to Upload a text file in Android

208 views Asked by At

The Code is given below,I am getting problem to upload a text file What I able to do -Authentication is working good -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);

In this Part having the error as NullPointerException in response object

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

Any Help Appreciated...Thanks in Advance.

1

There are 1 answers

0
sahu On BEST ANSWER

Here is the Solution for the above question which is asked by me itself.

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

->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 helpful to many people.Thanks