UPLOADING FILE TO DROPBOX USING DROPBOX ANDROID API
MainActivity.java
package app.sample.com;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.DropboxAPI.UploadRequest;
import com.dropbox.client2.android.AndroidAuthSession;
import com.dropbox.client2.android.AuthActivity;
import com.dropbox.client2.exception.DropboxException;
import com.dropbox.client2.exception.DropboxFileSizeException;
import com.dropbox.client2.exception.DropboxIOException;
import com.dropbox.client2.exception.DropboxParseException;
import com.dropbox.client2.exception.DropboxPartialFileException;
import com.dropbox.client2.exception.DropboxServerException;
import com.dropbox.client2.exception.DropboxUnlinkedException;
import com.dropbox.client2.session.AccessTokenPair;
import com.dropbox.client2.session.AppKeyPair;
import com.dropbox.client2.session.TokenPair;
import com.dropbox.client2.session.Session.AccessType;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
final String APP_KEY = "vzjha5o4swtoi53";
final String APP_SECRET = "cnb9z972bolzst7";
private DropboxAPI<AndroidAuthSession> mDBApi;
final static private String ACCOUNT_PREFS_NAME = "prefs";
final static private String ACCESS_KEY_NAME = "ACCESS_KEY";
final static private String ACCESS_SECRET_NAME = "ACCESS_SECRET";
final static private AccessType ACCESS_TYPE = AccessType.APP_FOLDER;
private Button btn_linkDropbox;
private boolean mLoggedIn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AndroidAuthSession session = buildSession();
mDBApi = new DropboxAPI<AndroidAuthSession>(session);
checkAppKeySetup();
setLoggedIn(mDBApi.getSession().isLinked());
}
@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;
}
protected void onResume()
{
try
{
super.onResume();
AndroidAuthSession session = mDBApi.getSession();
// The next part must be inserted in the onResume() method of the
// activity from which session.startAuthentication() was called, so
// that Dropbox authentication completes properly.
if (session.authenticationSuccessful())
{
//showToast("resume successfull..");
try {
// Mandatory call to complete the auth
session.finishAuthentication();
showToast("authentication finished succesfully");
// Store it locally in our app for later use
//String accessToken = mDBApi.getSession().getOAuth2AccessToken();
TokenPair tokens = session.getAccessTokenPair();
storeKeys(tokens.key, tokens.secret);
setLoggedIn(true);
}
catch (IllegalStateException e) {
showToast("Couldn't authenticate with Dropbox:" + e.getLocalizedMessage());
}
}
}
catch(Exception ex)
{
showToast("Resume ERROR .."+ex.getMessage());
}
}
private AndroidAuthSession buildSession() {
AppKeyPair appKeyPair = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session;
String[] stored = getKeys();
if (stored != null)
{
AccessTokenPair accessToken = new AccessTokenPair(stored[0], stored[1]);
session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE, accessToken);
//session= new AndroidAuthSession(appKeyPair, accessToken);
}
else
{
//session = new AndroidAuthSession(appKeyPair);
session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE);
}
return session;
}
private String[] getKeys()
{
SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
String key = prefs.getString(ACCESS_KEY_NAME, null);
String secret = prefs.getString(ACCESS_SECRET_NAME, null);
if (key != null && secret != null) {
String[] ret = new String[2];
ret[0] = key;
ret[1] = secret;
return ret;
} else {
return null;
}
}
private void setLoggedIn(boolean loggedIn)
{
btn_linkDropbox=(Button) findViewById(R.id.app);
mLoggedIn = loggedIn;
if (loggedIn)
{
btn_linkDropbox.setText("Unlink from Dropbox");
}
else
{
btn_linkDropbox.setText("Link with Dropbox");
}
}
private void storeKeys(String key, String secret) {
// Save the access key for later
SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
Editor edit = prefs.edit();
edit.putString(ACCESS_KEY_NAME, key);
edit.putString(ACCESS_SECRET_NAME, secret);
edit.commit();
}
public void uploaddata(View v)
{
try
{
// And later in some initialization function:
if(mDBApi.getSession().isLinked())
{
UploadFile uploadFile = new UploadFile(this, mDBApi);
uploadFile.execute();
showToast("File uploded successfully..");
}
else
{
showToast("Authenticating..");
//mDBApi.getSession().startOAuth2Authentication(MainActivity.this);
mDBApi.getSession().startAuthentication(MainActivity.this);
showToast("This app wasn't authenticated properly.Please link with dropbox to upload File");
}
}
catch(IllegalStateException ex)
{
showToast("Illegal state Exception Arises.."+ex.getMessage());
}
catch(Exception ex)
{
Toast.makeText(this,"ER GEN.."+ex.getMessage(), Toast.LENGTH_LONG).show();
Log.i("ER GEN..",ex.getMessage());
}
}
private void checkAppKeySetup()
{
// Check to make sure that we have a valid app key
if (APP_KEY.startsWith("CHANGE") ||
APP_SECRET.startsWith("CHANGE"))
{
showToast("You must apply for an app key and secret from developers.dropbox.com, and add them to the DBRoulette ap before trying it.");
finish();
return;
}
// Check if the app has set up its manifest properly.
Intent testIntent = new Intent(Intent.ACTION_VIEW);
String scheme = "db-" + APP_KEY;
String uri = scheme + "://" + AuthActivity.AUTH_VERSION + "/test";
testIntent.setData(Uri.parse(uri));
PackageManager pm = getPackageManager();
if (0 == pm.queryIntentActivities(testIntent, 0).size()) {
showToast("URL scheme in your app's " +
"manifest is not set up correctly. You should have a " +
"com.dropbox.client2.android.AuthActivity with the " +
"scheme: " + scheme);
Log.i("res","URL scheme in your app's " +
"manifest is not set up correctly. You should have a " +
"com.dropbox.client2.android.AuthActivity with the " +
"scheme: " + scheme);
finish();
}
}
private void showToast(String message)
{
Toast.makeText(this,message,Toast.LENGTH_LONG).show();
}
}
UploadFile.java
package app.sample.com;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.DropboxAPI.UploadRequest;
import com.dropbox.client2.android.AndroidAuthSession;
import com.dropbox.client2.android.AuthActivity;
import com.dropbox.client2.exception.DropboxException;
import com.dropbox.client2.exception.DropboxFileSizeException;
import com.dropbox.client2.exception.DropboxIOException;
import com.dropbox.client2.exception.DropboxParseException;
import com.dropbox.client2.exception.DropboxPartialFileException;
import com.dropbox.client2.exception.DropboxServerException;
import com.dropbox.client2.exception.DropboxUnlinkedException;
import com.dropbox.client2.session.AccessTokenPair;
import com.dropbox.client2.session.AppKeyPair;
import com.dropbox.client2.session.TokenPair;
import com.dropbox.client2.session.Session.AccessType;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
final String APP_KEY = "vzjha5o4swtoi53";
final String APP_SECRET = "cnb9z972bolzst7";
private DropboxAPI<AndroidAuthSession> mDBApi;
final static private String ACCOUNT_PREFS_NAME = "prefs";
final static private String ACCESS_KEY_NAME = "ACCESS_KEY";
final static private String ACCESS_SECRET_NAME = "ACCESS_SECRET";
final static private AccessType ACCESS_TYPE = AccessType.APP_FOLDER;
private Button btn_linkDropbox;
private boolean mLoggedIn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AndroidAuthSession session = buildSession();
mDBApi = new DropboxAPI<AndroidAuthSession>(session);
checkAppKeySetup();
setLoggedIn(mDBApi.getSession().isLinked());
}
@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;
}
protected void onResume()
{
try
{
super.onResume();
AndroidAuthSession session = mDBApi.getSession();
// The next part must be inserted in the onResume() method of the
// activity from which session.startAuthentication() was called, so
// that Dropbox authentication completes properly.
if (session.authenticationSuccessful())
{
//showToast("resume successfull..");
try {
// Mandatory call to complete the auth
session.finishAuthentication();
showToast("authentication finished succesfully");
// Store it locally in our app for later use
//String accessToken = mDBApi.getSession().getOAuth2AccessToken();
TokenPair tokens = session.getAccessTokenPair();
storeKeys(tokens.key, tokens.secret);
setLoggedIn(true);
}
catch (IllegalStateException e) {
showToast("Couldn't authenticate with Dropbox:" + e.getLocalizedMessage());
}
}
}
catch(Exception ex)
{
showToast("Resume ERROR .."+ex.getMessage());
}
}
private AndroidAuthSession buildSession() {
AppKeyPair appKeyPair = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session;
String[] stored = getKeys();
if (stored != null)
{
AccessTokenPair accessToken = new AccessTokenPair(stored[0], stored[1]);
session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE, accessToken);
//session= new AndroidAuthSession(appKeyPair, accessToken);
}
else
{
//session = new AndroidAuthSession(appKeyPair);
session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE);
}
return session;
}
private String[] getKeys()
{
SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
String key = prefs.getString(ACCESS_KEY_NAME, null);
String secret = prefs.getString(ACCESS_SECRET_NAME, null);
if (key != null && secret != null) {
String[] ret = new String[2];
ret[0] = key;
ret[1] = secret;
return ret;
} else {
return null;
}
}
private void setLoggedIn(boolean loggedIn)
{
btn_linkDropbox=(Button) findViewById(R.id.app);
mLoggedIn = loggedIn;
if (loggedIn)
{
btn_linkDropbox.setText("Unlink from Dropbox");
}
else
{
btn_linkDropbox.setText("Link with Dropbox");
}
}
private void storeKeys(String key, String secret) {
// Save the access key for later
SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
Editor edit = prefs.edit();
edit.putString(ACCESS_KEY_NAME, key);
edit.putString(ACCESS_SECRET_NAME, secret);
edit.commit();
}
public void uploaddata(View v)
{
try
{
// And later in some initialization function:
if(mDBApi.getSession().isLinked())
{
UploadFile uploadFile = new UploadFile(this, mDBApi);
uploadFile.execute();
showToast("File uploded successfully..");
}
else
{
showToast("Authenticating..");
//mDBApi.getSession().startOAuth2Authentication(MainActivity.this);
mDBApi.getSession().startAuthentication(MainActivity.this);
showToast("This app wasn't authenticated properly.Please link with dropbox to upload File");
}
}
catch(IllegalStateException ex)
{
showToast("Illegal state Exception Arises.."+ex.getMessage());
}
catch(Exception ex)
{
Toast.makeText(this,"ER GEN.."+ex.getMessage(), Toast.LENGTH_LONG).show();
Log.i("ER GEN..",ex.getMessage());
}
}
private void checkAppKeySetup()
{
// Check to make sure that we have a valid app key
if (APP_KEY.startsWith("CHANGE") ||
APP_SECRET.startsWith("CHANGE"))
{
showToast("You must apply for an app key and secret from developers.dropbox.com, and add them to the DBRoulette ap before trying it.");
finish();
return;
}
// Check if the app has set up its manifest properly.
Intent testIntent = new Intent(Intent.ACTION_VIEW);
String scheme = "db-" + APP_KEY;
String uri = scheme + "://" + AuthActivity.AUTH_VERSION + "/test";
testIntent.setData(Uri.parse(uri));
PackageManager pm = getPackageManager();
if (0 == pm.queryIntentActivities(testIntent, 0).size()) {
showToast("URL scheme in your app's " +
"manifest is not set up correctly. You should have a " +
"com.dropbox.client2.android.AuthActivity with the " +
"scheme: " + scheme);
Log.i("res","URL scheme in your app's " +
"manifest is not set up correctly. You should have a " +
"com.dropbox.client2.android.AuthActivity with the " +
"scheme: " + scheme);
finish();
}
}
private void showToast(String message)
{
Toast.makeText(this,message,Toast.LENGTH_LONG).show();
}
}
No comments:
Post a Comment