Is there any CloudRail sample only for GoogleDrive upload/download? I followed this video and this tuorial and managed to make it working for authentication (Really great tutorials, even for newbies). Then I tried to use github sample for cloud storage, it is working when using the github project but when I try to integrate to my app then the app is not getting CloudRail initial screen and stopping suddenly.
Also the github project is old and using deprecated methods and API for example
setDrawerListener,
Fragment as per android documentation
It would be great if the CouldRail team provide more tutorials like that.
EDIT- below is the source code
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();
}
}
Services.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>