Reputation: 1
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();
Any idea???
Upvotes: 0
Views: 654
Reputation: 6727
I think you can use AsyncTask to achieve your goal.
Upvotes: 1