cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Back again with another edition of 'How we use Dropbox', so find out how Emma uses to-do lists to get it all done here!

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

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

Demo B.
New member | Level 1

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 8

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

Greg-DB
Dropboxer

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"

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

Demo B.
New member | Level 1

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

 



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

Steve M.
Dropboxer

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?)

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

Demo B.
New member | Level 1

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

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

Demo B.
New member | Level 1
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.

 

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

Greg-DB
Dropboxer

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);

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

Demo B.
New member | Level 1
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.

}

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

Demo B.
New member | Level 1

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()

Poll
We love to learn from the educators who use Dropbox. Whether you teach kids, teens, adults or a combination of all three, we want to know what apps and integrations you use with Dropbox to help with teaching. Which of the ones below is your favorite, or most used tool?
Who's talking

Top contributors to this post

  • User avatar
    Demo B. New member | Level 1
  • User avatar
    Greg-DB Dropboxer
What do Dropbox user levels mean?
Need more support?