How to pick file from android device and send to server.Here is my mainactivity after running it will give onresume
activity error. Because after selection of file it gives nullpointer
at runtime exception.
btnUpload = (Button) findViewById(R.id.btnUploadPhoto);
btnUpload.setOnClickListener(this);
btnDownload.setOnClickListener(this);
}
private AndroidAuthSession buildSession() {
AppKeyPair appKeyPair = new AppKeyPair(Constants.DROPBOX_APP_KEY,
Constants.DROPBOX_APP_SECRET);
AndroidAuthSession session;
String[] stored = getKeys();
if (stored != null) {
AccessTokenPair accessToken = new AccessTokenPair(stored[0],
stored[1]);
session = new AndroidAuthSession(appKeyPair, Constants.ACCESS_TYPE,
accessToken);
} else {
session = new AndroidAuthSession(appKeyPair, Constants.ACCESS_TYPE);
}
return session;
}
private String[] getKeys() {
SharedPreferences prefs = getSharedPreferences(
Constants.ACCOUNT_PREFS_NAME, 0);
String key = prefs.getString(Constants.ACCESS_KEY_NAME, null);
String secret = prefs.getString(Constants.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;
}
}
@Override
public void onClick(View v) {
if (v == btnDownload) {
startActivity(new Intent(MainActivity.this, DropboxDownload.class));
} else if (v == btnUpload) {
createDir();
if (mLoggedIn) {
logOut();
}
// Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// f = new File(Utils.getPath(),new Date().getTime()+".jpg");
// intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
// intent.setType("file/*");
// startActivityForResult(intent,TAKE_PHOTO);
//Intent intent = new Intent(MediaStore.ACTION_GET_CONTENT);
// startActivityForResult(intent, TAKE_PHOTO);
/* Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, TAKE_PHOTO); */
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
Intent f = Intent.createChooser(intent, "File");
startActivityForResult(f, TAKE_PHOTO);
}
}
private void logOut() {
mApi.getSession().unlink();
clearKeys();
}
private void clearKeys() {
SharedPreferences prefs = getSharedPreferences(
Constants.ACCOUNT_PREFS_NAME, 0);
Editor edit = prefs.edit();
edit.clear();
edit.commit();
}
private void createDir() {
File dir = new File(Utils.getPath());
if (!dir.exists()) {
dir.mkdirs();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == TAKE_PHOTO) {
// f = new File(Utils.getPath() + "/temp.jpg");
if (Utils.isOnline(MainActivity.this)) {
mApi.getSession().startAuthentication(MainActivity.this);
onResume = true;
} else {
Utils.showNetworkAlert(MainActivity.this);
}
}
}
}
public void setLoggedIn(boolean loggedIn) {
mLoggedIn = loggedIn;
if (loggedIn) {
UploadFile upload = new UploadFile(MainActivity.this, mApi, DIR, f);
upload.execute();
onResume = true;
}
}
private void storeKeys(String key, String secret) {
SharedPreferences prefs = getSharedPreferences(
Constants.ACCOUNT_PREFS_NAME, 0);
Editor edit = prefs.edit();
edit.putString(Constants.ACCESS_KEY_NAME, key);
edit.putString(Constants.ACCESS_SECRET_NAME, secret);
edit.commit();
}
private void showToast(String msg) {
Toast error = Toast.makeText(this, msg, Toast.LENGTH_LONG);
error.show();
}
@Override
protected void onResume() {
AndroidAuthSession session = mApi.getSession();
if (session.authenticationSuccessful()) {
try {
session.finishAuthentication();
TokenPair tokens = session.getAccessTokenPair();
storeKeys(tokens.key, tokens.secret);
setLoggedIn(onResume);
} catch (IllegalStateException e) {
showToast("Couldn't authenticate with Dropbox:"
+ e.getLocalizedMessage());
}
}
super.onResume();
}
}
Here is my crash report:
01-25 13:30:20.629 9008-9008/com.example.pranit.cloudexample W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x419eacf8)
01-25 13:30:20.629 9008-9008/com.example.pranit.cloudexample W/dalvikvm: threadid=1: uncaught exception occurred
01-25 13:30:20.630 9008-9008/com.example.pranit.cloudexample W/System.err: java.lang.RuntimeException: Unable to resume activity {com.example.pranit.cloudexample/com.example.pranit.cloudexample.MainActivity}: java.lang.NullPointerException
01-25 13:30:20.631 9008-9008/com.example.pranit.cloudexample W/System.err: at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3003)
01-25 13:30:20.631 9008-9008/com.example.pranit.cloudexample W/System.err: at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3032)
01-25 13:30:20.631 9008-9008/com.example.pranit.cloudexample W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
01-25 13:30:20.632 9008-9008/com.example.pranit.cloudexample W/System.err: at android.os.Handler.dispatchMessage(Handler.java:110)
01-25 13:30:20.632 9008-9008/com.example.pranit.cloudexample W/System.err: at android.os.Looper.loop(Looper.java:193)
01-25 13:30:20.632 9008-9008/com.example.pranit.cloudexample W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5333)
01-25 13:30:20.632 9008-9008/com.example.pranit.cloudexample W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
01-25 13:30:20.632 9008-9008/com.example.pranit.cloudexample W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
01-25 13:30:20.632 9008-9008/com.example.pranit.cloudexample W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
01-25 13:30:20.632 9008-9008/com.example.pranit.cloudexample W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
01-25 13:30:20.633 9008-9008/com.example.pranit.cloudexample W/System.err: at dalvik.system.NativeStart.main(Native Method)
01-25 13:30:20.633 9008-9008/com.example.pranit.cloudexample W/System.err: Caused by: java.lang.NullPointerException
01-25 13:30:20.638 9008-9008/com.example.pranit.cloudexample W/System.err: at com.example.pranit.cloudexample.UploadFile.<init>(UploadFile.java:76)
01-25 13:30:20.638 9008-9008/com.example.pranit.cloudexample W/System.err: at com.example.pranit.cloudexample.MainActivity.setLoggedIn(MainActivity.java:162)
01-25 13:30:20.638 9008-9008/com.example.pranit.cloudexample W/System.err: at com.example.pranit.cloudexample.MainActivity.onResume(MainActivity.java:194)
01-25 13:30:20.639 9008-9008/com.example.pranit.cloudexample W/System.err: at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1197)
01-25 13:30:20.639 9008-9008/com.example.pranit.cloudexample W/System.err: at android.app.Activity.performResume(Activity.java:5422)
01-25 13:30:20.639 9008-9008/com.example.pranit.cloudexample W/System.err: at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2989)
01-25 13:30:20.640 9008-9008/com.example.pranit.cloudexample W/System.err: ... 10 more
01-25 13:30:20.640 9008-9008/com.example.pranit.cloudexample W/dalvikvm: threadid=1: calling UncaughtExceptionHandler
01-25 13:30:20.647 9008-9008/com.example.pranit.cloudexample E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.pranit.cloudexample, PID: 9008
java.lang.RuntimeException: Unable to resume activity {com.example.pranit.cloudexample/com.example.pranit.cloudexample.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3003)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3032)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.pranit.cloudexample.UploadFile.<init>(UploadFile.java:76)
at com.example.pranit.cloudexample.MainActivity.setLoggedIn(MainActivity.java:162)
at com.example.pranit.cloudexample.MainActivity.onResume(MainActivity.java:194)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1197)
at android.app.Activity.performResume(Activity.java:5422)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2989)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3032)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)