Your workflow is unique 👨‍💻 -  tell us how you use Dropbox here.

Forum Discussion

Demo B.'s avatar
Demo B.
New member | Level 1
10 years ago

How to access a directory programatically with java android code in dropbox

Not able to provide the correct path to upload a file programatically in dropbox.

putfile() can be used..But the path i am not able to provide . Searching for last 4 hours.Not able to find a solution.Please help if anyone can.

8 Replies

Replies have been turned off for this discussion
  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    10 years ago

    Can you clarify what you mean when you say you can't provide the path? The path should be the path in the Dropbox account where you want to upload the file. For example, to upload a file "test.txt" into a folder named "Documents", you would use the path "/Documents/test.txt"

  • Demo B.'s avatar
    Demo B.
    New member | Level 1
    10 years ago

    When using the way u r saying then I got an exception. DropboxException:null.

    try
    {
    DropboxAPI.Entry response = new DropboxAPI.Entry();
    Log.d("mhk","Uploading file..."+response.root);
    response = mDBApi.putFile(response.root+"/hello.txt", inputStream,file.length(), null, null);
    Log.d("mhk","File Uploaded..."+response);
    }
    catch (DropboxException e)
    {
    Log.e("mhk", "dropboxException is :" + e.getMessage());
    }

    causes exception:
    11-13 08:27:49.555 18716-18736/? D/mhk﹕ Uploading file...null
    11-13 08:27:49.555 18716-18736/? E/mhk﹕ dropboxException is :null

     



  • From the log output, it looks like response.root is null, which makes sense, since you're getting it from an empty DropboxAPI.Entry. What are you actually trying to do here? (Where do you want the file to go?)

  • Demo B.'s avatar
    Demo B.
    New member | Level 1
    10 years ago

    Want to upload a audio file .But first trying to upload a text file. In the folder :
    Apps/Mapplication
    in dropbox of the user using my android application

  • Demo B.'s avatar
    Demo B.
    New member | Level 1
    10 years ago
    private DropboxAPI<AndroidAuthSession> mDBApi;//global variable
    final static private String APP_KEY = "abc";
    final static private String APP_SECRET = "xyz";

    class forPutFile extends AsyncTask<String,String,Boolean>
    {
    @Override
    protected Boolean doInBackground(String... params)
    {
    Log.d("mhk","indoInBackground");
    File file = new File(Environment.getExternalStorageDirectory()+"/hello.txt");
    FileInputStream inputStream = null;
    Log.d("mhk","file created..");
    try
    {
    Log.d("mhk","updating inputstream.");
    inputStream = new FileInputStream(file);
    Log.d("mhk","inputstream is :"+inputStream);
    }
    catch (FileNotFoundException e)
    {
    Log.e("mhk", "inputstreamException is : " + e.getMessage());
    }

    try
    {
    DropboxAPI.Entry response = new DropboxAPI.Entry();
    Log.d("mhk","Uploading file..."+response.root);
    response = mDBApi.putFile(response.root+"/hello.txt", inputStream,file.length(), null, null);
    Log.d("mhk","File Uploaded..."+response);
    }
    catch (DropboxException e)
    {
    Log.e("mhk", "dropboxException is :" + e.getMessage());
    }
    Log.d("mhk", "The uploaded file's rev is: ");//+ response.rev);
    return false; }}
    void savingToDropbox(){ try {
    try
    {
    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
    AndroidAuthSession session = new AndroidAuthSession(appKeys);
    mDBApi = new DropboxAPI<AndroidAuthSession>(session);
    mDBApi.getSession().startOAuth2Authentication(MainActivity.this);
    new forPutFile().execute("Hii");
    }
    catch (Exception e1)
    {
    Log.e("mhk", "DropboxException" + e1.getMessage());
    }
    }
    catch (Exception e)
    {
    Log.e("mhk", "FileNotFoundException" + e.getMessage());
    }
    }

    protected void onResume()
    {
    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();


    }
    catch (IllegalStateException e)
    {
    Log.e("mhk", "Error authenticating", e);
    }
    }
    }

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
    return true;
    }

    return super.onOptionsItemSelected(item);
    }
    }
    11-13 08:27:49.385 18716-18736/? D/mhk﹕ indoInBackground
    11-13 08:27:49.385 18716-18736/? D/mhk﹕ file created..
    11-13 08:27:49.385 18716-18736/? D/mhk﹕ updating inputstream.
    11-13 08:27:49.505 18716-18736/? D/mhk﹕ inputstream is :java.io.FileInputStream@419d4e98
    11-13 08:27:49.555 18716-18736/? D/mhk﹕ Uploading file...null
    11-13 08:27:49.555 18716-18736/? E/mhk﹕ dropboxException is :null
    11-13 08:27:49.555 18716-18736/? D/mhk﹕ The uploaded file's rev is:

    This is the full snippet. But it is giving exception ... Having all those permissions and everything in manifest file.

     

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    10 years ago

    You're still using an empty DropboxAPI.Entry, like Steve mentioned before. To see how this works, I recommend just starting with a simple path. For example, try this to get started:

                response = mDBApi.putFile("/hello.txt", inputStream, file.length(), null, null);
  • Demo B.'s avatar
    Demo B.
    New member | Level 1
    10 years ago
    package com.example.mhk.dropbox;

    import android.content.Context;
    import android.content.SharedPreferences;
    import android.os.AsyncTask;
    import android.os.Environment;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.EditText;

    import com.dropbox.client2.DropboxAPI;
    import com.dropbox.client2.android.AndroidAuthSession;
    import com.dropbox.client2.exception.DropboxException;
    import com.dropbox.client2.session.AppKeyPair;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.io.IOException;

    public class MainActivity extends AppCompatActivity
    {
    private DropboxAPI<AndroidAuthSession> mDBApi;//global variable
    final static private String APP_KEY = "jkj";
    final static private String APP_SECRET = "xyx";

    class forPutFile extends AsyncTask<String,String,Boolean>
    {
    @Override
    protected Boolean doInBackground(String... params)
    {
    Log.d("mhk","indoInBackground");
    File file = new File(Environment.getExternalStorageDirectory()+"/hello.txt");
    FileInputStream inputStream=null;
    try
    {
    inputStream = new FileInputStream(file);
    Log.d("mhk","file created..");
    Log.d("mhk","inputstream is :"+inputStream);
    }
    catch (FileNotFoundException e)
    {
    Log.e("mhk", "inputstreamException is : " + e.getMessage());
    }
    try
    {
    DropboxAPI.Entry response = mDBApi.putFile("/hello.txt", inputStream,file.length(), null, null);
    Log.d("mhk","The uploaded file's rev is: " + response.rev);
    }
    catch (DropboxException e)
    {
    e.printStackTrace();
    Log.e("mhk", "dropboxException is :" + e.getMessage());
    }
    return false;
    }
    }

    void savingToDropbox()
    {
           try
    {
    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
    AndroidAuthSession session = new AndroidAuthSession(appKeys);
    mDBApi = new DropboxAPI<AndroidAuthSession>(session);
    mDBApi.getSession().startOAuth2Authentication(MainActivity.this);
    }
    catch (Exception e1)
    {
    Log.e("mhk", "Exception" + e1.getMessage());
    }

    }

    protected void onResume()
    {
    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();
    SharedPreferences sp=getSharedPreferences("DropBox App", MODE_PRIVATE);
    SharedPreferences.Editor ed=sp.edit();
    ed.putString("accessToken",accessToken);
    }
    catch (IllegalStateException e)
    {
    Log.e("mhk", "Error authenticating", e);
    }
    }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    savingToDropbox();
    new forPutFile().execute("Hii");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
    return true;
    }

    return super.onOptionsItemSelected(item);
    }
    This is the full code snippet i am using. Gregory had done by giving simple path also.Getting the same error giving under:
    11-16 14:49:57.977 22637-22675/? D/mhk﹕ indoInBackground
    11-16 14:49:57.987 22637-22675/? D/mhk﹕ file created..
    11-16 14:49:57.987 22637-22675/? D/mhk﹕ inputstream is :java.io.FileInputStream@41aba9f0
    11-16 14:49:57.997 22637-22675/? E/mhk﹕ dropboxException is :null (This is the exception...)
    Please provide a solution .I am studying Core API for the first time.And it had really irritated me.

    }
  • Demo B.'s avatar
    Demo B.
    New member | Level 1
    10 years ago

    I have found my bug.Thanks everyone for your support. I was calling putFile() at initialization. But it must be called after authentication and in onResume()

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!