如何在 android 中实施 Dropbox?我已成功实施 API 但它仍然无法正常工作
How to Implement Dropbox in android ?I have implemented the API successfully but it still not working
如果有人有示例程序,请在此处提供,以便在我们的项目中实施。
我是 Android 我面临的技术的新手 problem.If 如果有人需要代码,我会在这里提供。
提前致谢。
试试这个,
String file_name = "/my_file.txt";
String file_path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + file_name;
AndroidAuthSession session;
public void initDropBox() {
AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
session = new AndroidAuthSession(appKeys);
mDBApi = new DropboxAPI<AndroidAuthSession>(session);
mDBApi.getSession().startOAuth2Authentication(ChatActivity.this);
}
Entry response;
public void uploadFile() {
writeFileContent(file_path);
File file = new File(file_path);
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
response = mDBApi.putFile("/my_file.txt", inputStream,
file.length(), null, null);
Log.i("DbExampleLog", "The uploaded file's rev is: " + response.rev);
} catch (DropboxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void downloadFile() {
File file = new File(file_path);
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DropboxFileInfo info = null;
try {
info = mDBApi.getFile("/my_file.txt", null, outputStream, null);
Log.i("DbExampleLog", "The file's rev is: "
+ info.getMetadata().rev);
} catch (DropboxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (mDBApi.getSession().authenticationSuccessful()) {
try {
// Required to complete auth, sets the access token on the
// session
mDBApi.getSession().finishAuthentication();
String accessToken = mDBApi.getSession().getOAuth2AccessToken();
/**
* You'll need this token again after your app closes, so it's
* important to save it for future access (though it's not shown
* here). If you don't, the user will have to re-authenticate
* every time they use your app. A common way to implement
* storing keys is through Android's SharedPreferences API.
*/
} catch (IllegalStateException e) {
Log.i("DbAuthLog", "Error authenticating", e);
}
}
}
->在子线程中调用 uploadFile(...) 和 downLoadFile(...) 方法否则它会给你例外
-> 为此使用 AsyncTask 并在 doInBackground(...) 方法 .
中调用上述方法
希望,这将是 helpful.Thanks
如果有人有示例程序,请在此处提供,以便在我们的项目中实施。
我是 Android 我面临的技术的新手 problem.If 如果有人需要代码,我会在这里提供。
提前致谢。
试试这个,
String file_name = "/my_file.txt";
String file_path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + file_name;
AndroidAuthSession session;
public void initDropBox() {
AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
session = new AndroidAuthSession(appKeys);
mDBApi = new DropboxAPI<AndroidAuthSession>(session);
mDBApi.getSession().startOAuth2Authentication(ChatActivity.this);
}
Entry response;
public void uploadFile() {
writeFileContent(file_path);
File file = new File(file_path);
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
response = mDBApi.putFile("/my_file.txt", inputStream,
file.length(), null, null);
Log.i("DbExampleLog", "The uploaded file's rev is: " + response.rev);
} catch (DropboxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void downloadFile() {
File file = new File(file_path);
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DropboxFileInfo info = null;
try {
info = mDBApi.getFile("/my_file.txt", null, outputStream, null);
Log.i("DbExampleLog", "The file's rev is: "
+ info.getMetadata().rev);
} catch (DropboxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (mDBApi.getSession().authenticationSuccessful()) {
try {
// Required to complete auth, sets the access token on the
// session
mDBApi.getSession().finishAuthentication();
String accessToken = mDBApi.getSession().getOAuth2AccessToken();
/**
* You'll need this token again after your app closes, so it's
* important to save it for future access (though it's not shown
* here). If you don't, the user will have to re-authenticate
* every time they use your app. A common way to implement
* storing keys is through Android's SharedPreferences API.
*/
} catch (IllegalStateException e) {
Log.i("DbAuthLog", "Error authenticating", e);
}
}
}
->在子线程中调用 uploadFile(...) 和 downLoadFile(...) 方法否则它会给你例外 -> 为此使用 AsyncTask 并在 doInBackground(...) 方法 .
中调用上述方法希望,这将是 helpful.Thanks