1.在你的Android Studio中添加CloudRail的依赖:
compile 'com.cloudrail:cloudrail-si-android:2.22.2
2.初始化OneDrive
private final AtomicReference onedrive = new AtomicReference<>();
3.创建OneDrive对象,Client ID和Client Secret获得方法在后面介绍:
new OneDrive(context, "[Client ID]", "[Client Secret]");
Googledrive对象的new不需要Client secret,因为google官方的设定,无法从内置浏览器中登陆网盘,所以需要跳转到浏览器登陆,做隐式登陆后无需进行此操作.
/**
* @param context Provides a context to enable OAuth authorization flows, often 'this' can be used when within an Activity
* @param clientIdentifier The client identifier of your Google app
* @param clientSecret The client secret of your Google app, must be an empty string on this platform
* @param redirectUri The redirect URI. Must be your package name / bundle ID and any path you like
* @param state The state which can be used to prevent CSRF or transfer additional information about a user
*/
GoogleDrive(
Context context,
String clientIdentifier,
String clientSecret,
String redirectUri,
String state
);
--------------------------------------------------------------
new GoogleDrive(context,
"client id",//google api官网获取的凭证
"",
"com.cloudrail.fileviewer:/oauth2redirect",//冒号前面为项目包名,需要在google api上注册,冒号和后面为固定格式.为了挑战到外部浏览器后挑战回你自己的应用
"")
4.Onedrive对象set后可对其进行网盘操作:
onedrive.set(new OneDrive(context, "client id", "client secret"));
(到此为止已经集成了cloudrail环境,但是还不是隐式登陆,需要输入相应的账号密码才可以对onedrive进行操作,下面介绍如何获取隐式登陆token)
Log.i(TAG, "OneDrive token: " + onedrive.get().saveAsString());
(使用saveAsString()方法可获得onedrive的账号信息,做一次测试应用,在登陆账号后获得账号的token信息,保存账号信息,修改代码,在new对象后加载账号信息,可实现不登陆账号密码直接对账号进行操作)
InputStream is = context.getResources().openRawResource(R.raw.onedrive);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = br.readLine()) != null) {
OneDrive oneDrive = new OneDrive(context, "client id", "client secret");
oneDrive.loadAsString(line);
onedrive.set(oneDrive);
}
is.close();
5.网盘上传操作upload(上传文件的路径,文件内容的流,文件的大小,是否进行覆盖);一般都进行同文件名进行覆盖,false如出现同文件名则抛出异常.
/**
* @param path The destination path for the new file
* @param stream The file content that will be uploaded
* @param size The file size measured in bytes
* @param overwrite Indicates if the file should be overwritten. Throws an error if set to false and the specified file already exists
* @throws IllegalArgumentException Null or malformatted path, null stream or negative file size
*/
void upload(
String path,
InputStream stream,
Long size,
Boolean overwrite
);
6.传入文件路径后可创建共享连接,返回字符串,谁拿到连接都可以获得分享的内容(危险操作)
/**
* @param path The path to the file or folder to share
* @return The shareable link
* @throws IllegalArgumentException Attempt to share root
*/
String createShareLink(
String path
);
7.文件下载download(共享连接);返回以流的形式的文件内容,对其进行保存到本地操作.
public String download(String cloudFilePath) {
String tar_path = null;
try {
CloudStorage cs = getTargetedCloudStorageByUrl(cloudFilePath);//cs 只是网盘对象
InputStream is = cs.download(cloudFilePath);
long size = is.available();
File f = new File(context.getFilesDir(), "downloaded_" + UriUtils.getFileNameFromUrl(cloudFilePath));
String outpath = f.getPath();
FileOutputStream fos = new FileOutputStream(outpath);
byte[] b = new byte[1024];
while ((is.read(b)) != -1) {
fos.write(b);
}
is.close();
fos.close();
tar_path = f.getAbsolutePath();
} catch (FileNotFoundException e) {
errorMsg = e.getMessage();
e.printStackTrace();
return null;
} catch (IOException e) {
errorMsg = e.getMessage();
e.printStackTrace();
return null;
} catch (Exception e) {
errorMsg = e.getMessage();
e.printStackTrace();
return null;
}
return tar_path;
}
更多方法参考官方网站https://cloudrail.com/
注意:对于这种类型的应用程序,没有客户端的秘密,当使用SDK时,您需要传递一个空字符串作为秘密!