是否有任何 CloudRail 示例仅用于 GoogleDrive 上传/下载?我关注了这个视频和这个教程,并设法使它适用于身份验证(非常棒的教程,即使对于新手也是如此)。然后我尝试使用github 示例进行云存储,使用 github 项目时它可以工作,但是当我尝试集成到我的应用程序时,应用程序没有获得 CloudRail 初始屏幕并突然停止。
github项目也很旧,例如使用不推荐使用的方法和API
setDrawerListener,
根据android文档的片段
如果 CandRail 团队提供更多这样的教程,那就太好了。
编辑-下面是源代码
MainActivity.java
package com.mpathak.driverail;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.cloudrail.si.CloudRail;
public class MainActivity extends AppCompatActivity {
private static final String BROWSABLE = "android.intent.category.BROWSABLE";
FileInputStream fileInputStream = null;
File image = new File(Environment.getExternalStorageDirectory(),"/DCIM/Camera/someImage.jpg");
CloudStorage googleDriveService = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int Service_Number = 1;
Services.getInstance().prepare(this);
googleDriveService = Services.getInstance().getService(Service_Number);
new Thread(new Runnable() {
@Override
public void run() {
try {
fileInputStream = new FileInputStream(image);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String imagesavingPath = "/" + image.getName();
if (fileInputStream != null)
{
googleDriveService.upload(imagesavingPath, fileInputStream, image.length(), true);
}
fileInputStream = null;
}
});
setContentView(R.layout.activity_main);
}
@Override
protected void onNewIntent(Intent intent)
{
if(intent.getCategories().contains(BROWSABLE)){
CloudRail.setAuthenticationResponse(intent);
}
super.onNewIntent(intent);
}
@Override
protected void onStop() {
Services.getInstance().storePersistent();
super.onStop();
}
}
服务.java
package com.mpathak.driverail;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import com.cloudrail.si.CloudRail;
import com.cloudrail.si.exceptions.ParseException;
import com.cloudrail.si.interfaces.CloudStorage;
import com.cloudrail.si.services.GoogleDrive;
import java.util.concurrent.atomic.AtomicReference;
/**
* This class encapsulates the different services being used by the application. It also initializes
* them and persists the authentication data.
*
*/
public class Services {
private final static String CLOUDRAIL_LICENSE_KEY = "MY_CLOUDRAIL_KEY";
private final static Services ourInstance = new Services();
private final AtomicReference<CloudStorage> googledrive = new AtomicReference<>();
private Activity context = null;
static Services getInstance() {
return ourInstance;
}
private Services() {
}
private void initGoogleDrive() {
googledrive.set(new GoogleDrive(context, "MY_GOOGLE_ID",
"", "com.mpathak.driverail:/oauth2redirect", ""));
try
{
((GoogleDrive) googledrive.get()).useAdvancedAuthentication();
}
catch(Exception ex) {
}
}
// --------- Public Methods -----------
void prepare(Activity context) {
this.context = context;
CloudRail.setAppKey(CLOUDRAIL_LICENSE_KEY);
this.initGoogleDrive();
SharedPreferences sharedPreferences = context.getPreferences(Context.MODE_PRIVATE);
try {
String persistent = sharedPreferences.getString("googledrivePersistent", null);
if (persistent != null)
{
googledrive.get().loadAsString(persistent);
}
} catch (ParseException e) {}
}
CloudStorage getService(int service) {
AtomicReference<CloudStorage> ret = new AtomicReference<>();
switch (service) {
case 1:
ret = this.googledrive;
break;
default:
throw new IllegalArgumentException("Unknown service!");
}
return ret.get();
}
void storePersistent() {
SharedPreferences sharedPreferences = context.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("googledrivePersistent", googledrive.get().saveAsString());
editor.apply();
}
}
AndroidMenifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mpathak.driverail">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="com.mpathak.driverail" />
</intent-filter>
</activity>
</application>
</manifest>