Android 如何从Dropbox下载文件,我不知道';我不知道我做错了什么
请帮忙。。。下载功能一直在运行,直到Toast“Downloading…”,但不在此之后,并且在目标文件夹中创建了一个空文件。 我想dropbox一定没有经过身份验证,请查看。 我选择的源在我的应用程序文件夹中,该文件夹是在Dropbox developers中创建项目时生成的……请检查是否正确Android 如何从Dropbox下载文件,我不知道';我不知道我做错了什么,android,dropbox-api,Android,Dropbox Api,请帮忙。。。下载功能一直在运行,直到Toast“Downloading…”,但不在此之后,并且在目标文件夹中创建了一个空文件。 我想dropbox一定没有经过身份验证,请查看。 我选择的源在我的应用程序文件夹中,该文件夹是在Dropbox developers中创建项目时生成的……请检查是否正确 final static private String APP_KEY = "xxxxxx"; final static private String APP_SECRET = "xxx
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;
}
}
final static private String APP_KEY=“xxxxxx”;
最终静态私有字符串APP_SECRET=“xxxxxx”;
最终静态私有字符串访问\u TOKEN=“xxxxxx”;
公共静态字符串DropboxDownloadPathFrom=“MyFolder/getstartedwithdropbox.pdf”//这是我在应用程序文件夹中的源代码
公共静态字符串DropboxDownloadPathTo=“/storage/simulated/0/DropboxItems/hello”//这是我的目的地
公共静态字符串dropboxdirpath=“/storage/simulated/0/DropboxItems/”;
公共静态文件目录=新文件(dropboxdirpath);
//在类别声明部分:
静态私有DropboxAPI mDBApi;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppKeyPair appKeys=新的AppKeyPair(应用程序密钥,应用程序密钥);
AndroidAuthSession会话=新的AndroidAuthSession(appKeys);
session.setOAuth2AccessToken(ACCESS_TOKEN);//我正在使用ACCESS TOKEN,因此不需要其他身份验证
mDBApi=新的DropboxAPI(会话);
btn=(按钮)findViewById(R.id.Button);
btn.setOnClickListener(单击);
}
private View.OnClickListener click=new View.OnClickListener(){
@凌驾
公共void onClick(视图){
DownloadFromDropboxFromPath(DropboxDownloadPathTo,DropboxDownloadPathFrom);
}};
//这是下载功能
私有void DownloadFromDropboxFromPath(字符串downloadPathTo,字符串downloadPathFrom)
DropboxDownloadPathTo=下载路径To;
DropboxDownloadPathFrom=下载路径From;
private void DownloadFromDropboxFromPath()下载{
{dir.mkdirs();
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),“下载文件…”,Toast.LENGTH\u SHORT.show();
Thread th=新线程(new Runnable(){
公开募捐{
File File=new File(DropboxDownloadPathTo+DropboxDownloadPathFrom.substring(DropboxDownloadPathFrom.lastIndexOf('.'));
如果(file.exists())file.delete();
试一试{
FileOutputStream outputStream=新的FileOutputStream(文件);
MainActivity.mDBApi.getFile(DropboxDownloadPathFrom,null,outputStream,null);
getMain().runOnUiThread(新的Runnable()){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),“文件已成功下载。”,Toast.LENGTH\u SHORT.show();
}
});
}捕获(例外e){
e、 printStackTrace();
}
}
});
th.start();
}
});
}
}
公共main活动getMain()
{
归还这个;
}
}
看起来您正在使用drpobox api v1,但从2017年9月28日起,dropbox api v1不再是可访问的端点,因此您必须使用dropbox api v2。看起来您正在使用drpobox api v1,但从2017年9月28日起,dropbox api v1不再是可访问的端点,因此您必须使用dropbox api v2