请帮助...下载功能一直运行到Toast“Downloading ..”但之后没有,并且在目标文件夹中创建了一个空文件。 我认为Dropbox一定不能通过身份验证,请参阅。 我选择的源是在我的app文件夹里面,这是在Dropbox开发人员创建项目时生成的......请检查它是否正确
final static private String APP_KEY = "xxxxxx";
final static private String APP_SECRET = "xxxxxx";
final static private String ACCESS_TOKEN = "xxxxxx";
public static String DropboxDownloadPathFrom = "MyFolder/Get Started with Dropbox.pdf"; //This is my source inside the app folder
public static String DropboxDownloadPathTo = "/storage/emulated/0/DropboxItems/hello"; //This is my destination
public static String dropboxdirpath="/storage/emulated/0/DropboxItems/";
public static File dir=new File(dropboxdirpath);
// In the class declaration section:
static private DropboxAPI<AndroidAuthSession> mDBApi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appKeys);
session.setOAuth2AccessToken(ACCESS_TOKEN); //I am using access token so no need for other authentication
mDBApi = new DropboxAPI<AndroidAuthSession>(session);
btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(click);
}
private View.OnClickListener click=new View.OnClickListener() {
@Override
public void onClick(View view) {
DownloadFromDropboxFromPath(DropboxDownloadPathTo,DropboxDownloadPathFrom);
}};
//This is the download function
private void DownloadFromDropboxFromPath (String downloadPathTo, String downloadPathFrom)
DropboxDownloadPathTo = downloadPathTo;
DropboxDownloadPathFrom = downloadPathFrom;
private void DownloadFromDropboxFromPath(){
{ dir.mkdirs();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Download file ...", Toast.LENGTH_SHORT).show();
Thread th = new Thread(new Runnable() {
public void run() {
File file = new File(DropboxDownloadPathTo + DropboxDownloadPathFrom.substring(DropboxDownloadPathFrom.lastIndexOf('.')));
if (file.exists()) file.delete();
try {
FileOutputStream outputStream = new FileOutputStream(file);
MainActivity.mDBApi.getFile(DropboxDownloadPathFrom, null, outputStream, null);
getMain().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "File successfully downloaded.", Toast.LENGTH_SHORT).show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
th.start();
}
});
}
}
public MainActivity getMain()
{
return this;
}
}