0
votes

I want upload image and file to DropBox, it's working fine but when I open the uploaded image and file from DropBox it is not opened. It shows open failed. How can I solve this problem? My code looks like this:

MainActivity.java

    public class MainActivity extends AppCompatActivity {

        private Toolbar toolbar;
        private FragmentManager fragmentManager = null;
        private FragmentTransaction fragmentTransaction = null;
        private DirectoryFragment mDirectoryFragment;
        private DropboxAPI<AndroidAuthSession> dropbox;
        private final static String APP_KEY = "APP_KEY";
        private final static String APP_SECRET = "APP_SECRET";
        private static final int PICKFILE_REQUEST_CODE = 1;
        private String Path = "";
        private int COME_FROM = 0;

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

            AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
            AndroidAuthSession session = new AndroidAuthSession(appKeys);
            dropbox = new DropboxAPI<AndroidAuthSession>(session);
            dropbox.getSession().startOAuth2Authentication(MainActivity.this);

            toolbar = (Toolbar) findViewById(R.id.tool_bar);
            toolbar.setTitle("Directory");
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();

            mDirectoryFragment = new DirectoryFragment();
            mDirectoryFragment.setDelegate(new DocumentSelectActivityDelegate() {

            @Override
            public void startDocumentSelectActivity() {

            }

            @Override
            public void didSelectFiles(DirectoryFragment activity,
                                       ArrayList<String> files) {               
                Log.d("Selected Files::::", files.get(0).toString());
                Path = files.get(0).toString();

                if (!Path.equals("")) {
                    Log.d("Path::::", Path);
                    FileInputStream inputStream = null;
                    try {
                        inputStream = new FileInputStream(Path);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    DropboxAPI.Entry response = null;
                    try {
                        response = dropbox.putFile(Path, inputStream,
                                Path.length(), null, null);
                    } catch (DropboxException e) {
                        e.printStackTrace();
                    }
                    Toast.makeText(MainActivity.this, Path + " Upload file successfull",
                            Toast.LENGTH_LONG)
                            .show();
                } else {
                    Toast.makeText(MainActivity.this, "Picked file failed",
                            Toast.LENGTH_LONG)
                            .show();
                }

            }

            @Override
            public void updateToolBarName(String name) {
                toolbar.setTitle(name);

            }
        });
        fragmentTransaction.add(R.id.fragment_container, mDirectoryFragment, "" + mDirectoryFragment.toString());
        fragmentTransaction.commit();

    }

    @Override
    protected void onDestroy() {
        mDirectoryFragment.onFragmentDestroy();
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        if (mDirectoryFragment.onBackPressed_()) {
            super.onBackPressed();
        }
    }

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

        if (dropbox.getSession().authenticationSuccessful()) {
            try {
                // Required to complete auth, sets the access token on the session
                dropbox.getSession().finishAuthentication();

                String accessToken = dropbox.getSession().getOAuth2AccessToken();
            } catch (IllegalStateException e) {
                Log.i("DbAuthLog", "Error authenticating", e);
            }
        }
    }
}
1

1 Answers

0
votes
response = dropbox.putFile(Path, inputStream,
    Path.length(), null, null);

The third parameter should be the length of the file, not the length of the path. If you check the size of the uploaded file, I think you'll find it's only a few bytes (whatever the length is of Path).

I think something like this might work:

response = dropbox.putFile(Path, inputStream,
    new File(Path).length(), null, null);