cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox 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: 

Get a list of files from dropbox to android application

Get a list of files from dropbox to android application

AppDev
Explorer | Level 3

Hi everybody, I'm new to dropbox developing and I need help with it please. I wrote an android application that needs to get a list of files listing in dropbox. I used the tutorial and part of the code I wrote is:

 

private DropboxAPI<AndroidAuthSession> mDBApi;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); InitializeSession(); } void InitializeSession() { AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET); AndroidAuthSession session = new AndroidAuthSession(appKeys);
mDBApi = new DropboxAPI<AndroidAuthSession>(session);
mDBApi.getSession().startOAuth2Authentication(MainActivity.this); } @Override protected void onResume() { super.onResume(); if (mDBApi.getSession().authenticationSuccessful()) { try {
mDBApi.getSession().finishAuthentication(); String accessToken = mDBApi.getSession().getOAuth2AccessToken(); PopulateList(); } catch (IllegalStateException e) { System.out.println("Error : " + e.getMessage()); } } } private void PopulateList() { List<String> filename = new ArrayList<>(); String mPath = "/"; DropboxAPI.Entry dirent = null; try {
dirent = mDBApi.metadata(mPath, 1000, null, true, null); } catch (DropboxException e) { System.out.println("Error : " + e.getMessage()); } for (DropboxAPI.Entry ent : dirent.contents) { if (ent.isDir) {
filename.add(ent.fileName()); } ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, filename); } }

 I am not getting the list of files from dropbox...

 In the PopulateList() method, after executing the line 

dirent = mDBApi.metadata(mPath, 1000, null, true, null);

The code stop executing and I get nothing to the dirent.

What am I doing wrong here? and what am I missing?

Do I somehow need to resume the authentication again?

One more thing I'm not sure about, with this code do I get files from sub folders too? if not, how can I get them? because the start path I use is "/".

 

I'm stuck on that for 2 weeks and can't find the solution or what did I do wrong...

Thanks for the helpers! 🙂

16 Replies 16

AppDev
Explorer | Level 3

Hi Greg, 🙂

Thank you so much for all your help! From what I read about the NetworkOnMainThreadException, I decided to implement what I need with IntentService and ResultReceive, and it is working perfectly fine now! 🙂

One more thing I still not sure about is the installation of the SDK. Now it is working for me because I added the jar manually. From the link you gave me, I don't understand how to use android studio package manager to download and install it automatically... Do you have a tutorial link that explane how to do that? All I tried didn't work. Sorry if it basic stuff, I realy am a begginer to this world... 😞

Greg-DB
Dropbox Staff

That's more about using Gradle/Maven/Android Studio themselves, so I can't offer much help, as that's outside the scope of Dropbox API support.

That said, it looks like their respective official documentation sites have some useful information:

AppDev
Explorer | Level 3

Thanks Greg! 🙂

One last thing that is not working for me yet, is the download of selected files. In my app, I got a list of all files and the user chose from that list which files he wants to download. Now I want to download all those files in a loop from the IntentService I wrote. A part of my code is:

@Override
protected void onHandleIntent(Intent intent)
{
if (intent != null)
{
ResultReceiver receiver = intent.getParcelableExtra("receiverTag");
String accessToken = intent.getStringExtra("AccessTokenVal");

DbxRequestConfig config = DbxRequestConfig.newBuilder("dropbox/MyAppName").build();
DbxClientV2 client = new DbxClientV2(config, accessToken);

if (intent.getAction().equals("GET_FILES"))
{
// The code to get the list of files
}
else if (intent.getAction().equals("Download_FILES"))
{
List<String> filesToDownload = new ArrayList<>();

Gson gson = new Gson();
Type type = new TypeToken<ArrayList<String>>(){}.getType();
filesToDownload = gson.fromJson(intent.getStringExtra("DropboxFilesSelected"), type);

boolean isAllFilesDownloaded = true;
for (int i=0; i<filesToDownload.size(); i++)
{
try
{
DbxDownloader<FileMetadata> downloader = client.files().download(filesToDownload.get(i));
String[] arr = filesToDownload.get(i).split("/");
FileOutputStream outputStream = new FileOutputStream(Environment.getExternalStorageDirectory() + "/MyAppName/MyAppFolder/" + arr[arr.length - 1]);

downloader.download(outputStream);
}
catch (DbxException e)
{
e.printStackTrace();
isAllFilesDownloaded = false;
}
catch (FileNotFoundException e)
{
e.printStackTrace();
isAllFilesDownloaded = false;
}
catch (IOException e)
{
e.printStackTrace();
isAllFilesDownloaded = false;
}
}
}
}

 The code failes in the line:

DbxDownloader<FileMetadata> downloader = client.files().download(filesToDownload.get(i));

I even tried to download a file from the root and wrote the path as:

DbxDownloader<FileMetadata> downloader = client.files().download("/file.txt");

 In the Logcat, this is the error I get:

E/AndroidRuntime: FATAL EXCEPTION: IntentService[DropboxActions]
Process: my project name, PID: 4118
java.lang.IllegalArgumentException: String 'path' does not match pattern
at com.dropbox.core.v2.files.DownloadArg.<init>(DownloadArg.java:42)
at com.dropbox.core.v2.files.DownloadArg.<init>(DownloadArg.java:69)
at com.dropbox.core.v2.files.DbxUserFilesRequests.download(DbxUserFilesRequests.java:1120)
at my project name.DropboxActions.onHandleIntent(DropboxActions.java:111)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:67)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.os.HandlerThread.run(HandlerThread.java:61)

 

Why is my path getting IllegalArgumentException? from what I read, I understood that a path to file in the root should be "/file.txt" and from a folder "/folder/file.txt".

What is wrong with my code?

Thanks again. 🙂

Greg-DB
Dropbox Staff

@AppDev That exception does indicate that the 'path' value you're supplying to the `download` method doesn't have the right format for a Dropbox path.

Can you double check that you're getting that same exact error for both versions of the line you pointed out? The second one, with the value "/file.txt" looks correct. That is a valid path format, and it is working for me.

For the first one, please print out the value of `filesToDownload.get(i)` just before that call to `download` to verify exactly what is being passed in.

AppDev
Explorer | Level 3

I printed the value of `filesToDownload.get(i)` before calling the download, and the value is "/file.txt".

For both versions I'm getting that error... 😞

The values I'm getting to the list `filesToDownload', is from the dropbox method for listing the list of files in dropbox, but I double checked it again and I'm grtting the correct value "/file.txt". I verified that the file is there in the root.

There might be a problem using 'DbxDownloader<FileMetadata>'? is there anything else I can use to download a file?

 

Greg-DB
Dropbox Staff

That's the correct way to download a file. I just tried that line of code myself and it is working as expected for me.

It sounds like there's something else affecting this in your project, but I can't say off hand what that might be. We'll be happy to look into it further, but we'd need to be able to reproduce it here. Can you share a small sample project that reproduces the issue?

AppDev
Explorer | Level 3

Hi Greg,

Sorry, it was my mistake... I didn't use Gson correctly, so when using it, I got inside the list the valuses ""/file1.txt"", ""/file2.txt"" etc. when I printed the values, I got "/file1.txt", "/file2.txt" instead of /file1.txt, /file2.txt. I thought that the extra "" are from the java println and that I'm getting the correct value...

Now everything is working for me and the files are downloading to my app.

I have no idea why the line with the hard coded file name didn't work before,

DbxDownloader<FileMetadata> downloader = client.files().download("/file.txt");

 But it is working now, so all is good.

Thank you so much for helping me with everything! 🙂

 

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    AppDev Explorer | Level 3
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?