You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.
Forum Discussion
Nicolas L.
11 years agoNew member | Level 1
Download file from Dropbox to local android Java
I spent some days on this code without finding the solution. Could you help me please?
I want to download a file from a Dropbox account to the application storage (local memory). When I open the file for the first time, the application crashes then when I restart the application, the file can be open from the local memory.
private void ImportDB(String nameDB)
{
try {
File data = Environment.getDataDirectory();
//Local file
String backupDBPath = "//data//[Package name]//databases//"+nameDB;
File backupDB = new File(data, backupDBPath);
//Dropbox file
DbxPath testPath = new DbxPath(DbxPath.ROOT, nameDB);
DbxFileSystem dbxFs = DbxFileSystem.forAccount(mDbxAcctMgr.getLinkedAccount());
DbxFile testFile = dbxFs.open(testPath);
FileChannel src = testFile.getReadStream().getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
//Transfert to local
dst.transferFrom(src, 0, src.size());
//close
src.close();
dst.close();
testFile.close();
}
catch (Exception e)
{
}
}
I guess it is a question of synchronization: the file is not yet copied when I try to open it. I tried this code for waiting the file but it doesn't work, the program stays in the while loop:
Thread mThread = new Thread()
{
@SuppressLint("NewApi") @Override
public void run()
{
//open the local file
File data = Environment.getDataDirectory();
String currentDBPath = "//data//[Package name]//databases//"+nameFileDB";
File currentDB = new File(data, currentDBPath);
// test when the file is downloaded
while (!currentDB.exists())
{
Log.i("tag", "In loop");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//CODE TO OPEN THE FILE HERE
}
}
};
mThread.start();
I already tried the AsyncTask and I have the same problem. I guess when the download from Dropbox is ordered, the program goes to the onPostExecute but it doesn't mean that the downloading is finished. I think I must have a listener on when the file is downloaded to the local memory.
Any Ideas???
- Steve M.Dropbox Staff
"When I open the file for the first time, the application crashes..." What's the stack trace of the crash?
- Nicolas L.New member | Level 1
Hello Steve,
Thank you for your answer. Actually the program import first the files to the local folder then it open the files. These files are database files.
The program crashes because it cannot find the local files. But this problem occurs only after the first run. I mean if I uninstall the application then I re install, it crashes. But if I just restart after the crash, it works, the local files were downloaded.Here the error stack trace:
12-31 10:07:46.461: E/AndroidRuntime(24240): FATAL EXCEPTION: main
12-31 10:07:46.461: E/AndroidRuntime(24240): Process: com.wataiso.labnotebook, PID: 24240
12-31 10:07:46.461: E/AndroidRuntime(24240): java.lang.RuntimeException: Unable to resume activity {com.wataiso.labnotebook/com.wataiso.labnotebook.TableauDeBord}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
12-31 10:07:46.461: E/AndroidRuntime(24240): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2791)
12-31 10:07:46.461: E/AndroidRuntime(24240): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2820)
12-31 10:07:46.461: E/AndroidRuntime(24240): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
12-31 10:07:46.461: E/AndroidRuntime(24240): at android.os.Handler.dispatchMessage(Handler.java:102)
12-31 10:07:46.461: E/AndroidRuntime(24240): at android.os.Looper.loop(Looper.java:136)
12-31 10:07:46.461: E/AndroidRuntime(24240): at android.app.ActivityThread.main(ActivityThread.java:5050)
12-31 10:07:46.461: E/AndroidRuntime(24240): at java.lang.reflect.Method.invokeNative(Native Method)
12-31 10:07:46.461: E/AndroidRuntime(24240): at java.lang.reflect.Method.invoke(Method.java:515)
12-31 10:07:46.461: E/AndroidRuntime(24240): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:805)
12-31 10:07:46.461: E/AndroidRuntime(24240): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:621)
12-31 10:07:46.461: E/AndroidRuntime(24240): at dalvik.system.NativeStart.main(Native Method)
12-31 10:07:46.461: E/AndroidRuntime(24240): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
12-31 10:07:46.461: E/AndroidRuntime(24240): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
12-31 10:07:46.461: E/AndroidRuntime(24240): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
12-31 10:07:46.461: E/AndroidRuntime(24240): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
12-31 10:07:46.461: E/AndroidRuntime(24240): at com.wataiso.labnotebook.DatabaseHelper.getInfo(DatabaseHelper.java:477)
12-31 10:07:46.461: E/AndroidRuntime(24240): at com.wataiso.labnotebook.TableauDeBord.showLinkedViewDB(TableauDeBord.java:607)
12-31 10:07:46.461: E/AndroidRuntime(24240): at com.wataiso.labnotebook.TableauDeBord.onResume(TableauDeBord.java:226)
12-31 10:07:46.461: E/AndroidRuntime(24240): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
12-31 10:07:46.461: E/AndroidRuntime(24240): at android.app.Activity.performResume(Activity.java:5310)
12-31 10:07:46.461: E/AndroidRuntime(24240): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2781)
12-31 10:07:46.461: E/AndroidRuntime(24240): ... 10 more - Steve M.Dropbox Staff
In general, to download a file, you need to open it and hold it open while waiting for the download to complete. (Everything in the Sync SDK is asynchronous.)
So the usual pattern here is to open the file, register a listener on that file, wait for
getNewerStatus().isCached
to be true, and then callupdate()
on the file. At that point, you should be able to read the latest file contents. (If you read before that, you'll either see cached data if available or you'll perhaps hang the UI thread waiting for network I/O.)I would actually expect your original code to work (but hang the thread), since
getReadStream()
should block if there's no cached data.Can you confirm that after
dst.transferFrom(src, 0, src.size());
, the file is empty (or there is some other problem with it) to narrow down the issue?
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.5,917 PostsLatest Activity: 2 hours ago
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 or Facebook.
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!