我花了几天时间研究这段代码,但没有找到解决方案。你能帮我一下吗?
我想将文件从 Dropbox 帐户下载到应用程序存储(本地内存)。当我第一次打开文件时,应用程序崩溃,然后当我重新启动应用程序时,可以从本地内存打开该文件。
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)
{
}
}
我猜这是同步问题:当我尝试打开文件时,文件尚未复制。我尝试使用这段代码来等待文件,但它不起作用,程序停留在 while 循环中:
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();
有什么想法吗???
最佳答案
我认为你可以使用AsyncTask实现您的目标。
- 从 Dropbox 下载 doInBackground 中的文件.
- onPostExecute文件下载完成时调用。
关于java - 从 Dropbox 下载文件到本地 android Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27666375/