1

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) 
Ankur Aggarwal
  • 2,210
  • 2
  • 34
  • 39
Pranit Bhor
  • 136
  • 2
  • 13
  • To give you a great answer, it might help us if you have a glance at [ask] if you haven't already. It might be also useful if you could provide a [mcve]. – Mat Jan 25 '17 at 07:59

2 Answers2

0

If your device/emulator > api 24

This crash generated due to permission issue as new rule of android M and N. So if you are working for file picker please put code for ask permission to user regarding android.permission.WRITE_EXTERNAL_STORAGE and android.permission.READ_EXTERNAL_STORAGE

or refere Android 6.0 Marshmallow. Cannot write to SD Card

else Share crash log.

Community
  • 1
  • 1
Arpan24x7
  • 648
  • 5
  • 24
0

@Pranit

try with

@Override
protected void onResume() {
         super.onResume();

    AndroidAuthSession session = mApi.getSession();
   try {
    if (session.authenticationSuccessful()) {

            session.finishAuthentication();

            TokenPair tokens = session.getAccessTokenPair();
            storeKeys(tokens.key, tokens.secret);
            setLoggedIn(onResume);
          }
        } catch (IllegalStateException e) {
            showToast("Couldn't authenticate with Dropbox:"
                    + e.getLocalizedMessage());
        }


}
Arpan24x7
  • 648
  • 5
  • 24
  • actually i am doing dropbox integration so i have code but it uploaded only camera capture image. I want to select any type of file from device ans upload it to dropbox. So help me – Pranit Bhor Jan 25 '17 at 10:25
  • I m not using is but here some link which may be help for you https://www.numetriclabz.com/upload-files-to-dropbox-using-core-api-android-tutorial/ or http://www.theappguruz.com/blog/10-steps-integrate-dropbox-api-android or http://stacktips.com/tutorials/android/share-file-to-dropbox-in-android-example – Arpan24x7 Jan 25 '17 at 10:44