The official java client for google drive API is hard of very poor design. There is a need for a wrapper and here it is. This library provides you the high-level abstraction to access the API. just a simple method call with only the required argument.
- Easy and simple
- Auto manage of credentials
- Supports Downloading/Uploading file (with progress)
- Supports Creating/Deleting file
- Supports Getting file meta-info
- More features coming soon...
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add the dependency, in module build. Gradle
dependencies {
implementation 'com.github.ErrorxCode:EasyDrive:1.2'
}
Declaring jitpack repository
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Adding the dependencies
<dependency>
<groupId>com.github.ErrorxCode</groupId>
<artifactId>EasyDrive</artifactId>
<version>Tag</version>
</dependency>
Altho using this library is very very easy, still, you can check some examples
Before using the library, you need to authenticate it to the user's drive. Provide your credentials passing them to the constructor to get a reference of the class.
try {
EasyDrive drive = new EasyDrive(CLIENT_ID,CLIENT_SECRET,REFRESH_CODE);
} catch (GeneralSecurityException | IOException e) {
e.printStackTrace();
}
You can upload a file to drive using 2 ways. Either directly with File
or with bytes[]
Example:
drive.uploadFile(filename,inputStream,null, new ProgressListener() {
@Override
public void onProgress(int percentage) {
}
@Override
public void onFinish(@Nullable String fileId) {
// The id of the file uploaded
}
@Override
public void onFailed(@Nonnull Exception e) {
}
});
Here, the 3nd argument is the folder id in which you want to upload the file.
null
means in the root.
Downloading files is also as easy as uploading them. See the example below
Example:
drive.download(fileId,"D://Downloads", new ProgressListener() {
@Override
public void onProgress(int percentage) {
}
@Override
public void onFinish(@Nullable String fileId) {
// null in this case
}
@Override
public void onFailed(@Nonnull Exception e) {
}
});
If you don't know the file id but know the name & parent folder, you can get
the id using getFileId(name,folder)
method.
Now every method except the above 2, returns an AsyncTask
object.
This is similar to the Future you get from ExecutorService or in a better way,
it is like a Task
you get from firebase calls. To learn more about AsyncTask, click the link
given a task, you can set the callbacks to get the result
Example:
task.setOnSuccessCallback(result -> {
// your callable return value is here
}).setOnErrorCallback(e -> {
// your callable exception is here
});
You can directly create text files in the drive using the creative()
method.
Or folder with createFolder()
.
Example:
AsyncTask<String> fileTask = drive.createTxtFile("file.txt", "Hello World", null);
AsyncTask<String> folderTask = drive.createFolder("folder", null);
The null
is the folder id where you want to create the file, "root" in this case.
You can get meta info of the file using the getX()
method where X
is the field (like size, name, owner, etc..) you want to fetch.
Example:
AsyncTask<Long> size = drive.getFileSize(fileId);
AsyncTask<String> name = drive.getName(fileId);
AsyncTask<String> id = drive.getFileId("test.txt", null);
drive.getAsInputStream(fileId);
Contributions are always welcome!
There is a scope for improvement in this library. What you can always do is you can add more API methods to the library.
If you liked my hard work, you can show your support. I don't take donations, you can star this repo instead.