(Posted on behalf of the question author).
Here is the answer to all my questions. The questions were
- Create a folder on the Google drive with a desired name.
- Upload a file (image/audio/video) to that particular folder.
Let's start with the point #1. Here is the working code to create a folder on the Google drive with a desired name. I would like to mention one more thing that is related to OAuth 2.0 authorization. As per Google guidance I have used the code from Codelab. Here is the link to get the code https://codelabs.developers.google.com/codelabs/appauth-android-codelab/?refresh=1#0 The scope should be the API provided by the Google for that particular service. For me it is "https://www.googleapis.com/auth/drive.file".
String metaDataFile = "{\"name\": \"folderName\","+ "\"mimeType\": \"application/vnd.google-apps.folder\"}";
RequestBody requestBodyMetaData = RequestBody.create(MediaType.parse("application/json; charset=UTF-8"), metaDataFile);
Request request = new Request.Builder()
.url("https://www.googleapis.com/drive/v3/files?")
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", String.format("Bearer %s", accessToken))
.post(requestBodyMetaData)
.build();
Response response = null;
OkHttpClient client = new OkHttpClient();
try {
response = client.newCall(request).execute();
successCode = String.valueOf(response.code());
}catch (IOException e){
e.printStackTrace();
}
Now you have to get the folder id. Here is the code to get the folder id.
Request request = new Request.Builder()
.url("https://www.googleapis.com/drive/v3/files")
.addHeader("Authorization", String.format("Bearer %s", accessToken))
.addHeader("Accept", "application/json")
.build();
Response response = null;
OkHttpClient client = new OkHttpClient();
try {
response = client.newCall(request).execute();
String jsonFile = response.body().string();
JSONObject jsonObject = new JSONObject(jsonFile);
JSONArray jsonArray = jsonObject.getJSONArray("files");
for (int i=0; i<jsonArray.length(); i++){
JSONObject json = jsonArray.getJSONObject(i);
String fileName = json.getString("name");
if (fileName.equalsIgnoreCase("folderName")) {
folderId = json.getString("id");
if (!folderId.equalsIgnoreCase(""))
preferences.setFolderCreated(true, folderId);
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
catch (JSONException e){
e.printStackTrace();
}
catch (NullPointerException e){
e.printStackTrace();
}
This folder id is needed to identify the folder where we are going to upload the file. Select the file from your list and then pass that file in the byte[] format to this code. Here we have to use mediatype as multipart because if we use simple upload (media) we cannot set a desired name to the uploaded file.
String metaDataFile = "{\"name\":\"uploadFileName\"," + "\"parents\" : [\""+ pref.getFolderId()+"\"]}"; // json type metadata
//attaching metadata to our request object
RequestBody requestBodyMetaData = RequestBody
.create(MediaType.parse("application/json; charset=UTF-8"), metaDataFile);
RequestBody body = RequestBody.create(MediaType.parse("audio/mp4"), file);
String size = String.valueOf(file.length);
//passing both meta data and file content for uploading
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("Metadata", null, requestBodyMetaData)
.addFormDataPart("Media", null, body)
.build();
//Requesting the api
Request request = new Request.Builder()
.url("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart")
.addHeader("Authorization", String.format("Bearer %s", accessToken))
.addHeader("Content-Type", "multipart/related; boundary=100")
.addHeader("Content-Length", size)
.addHeader("Accept", "application/json")
.post(requestBody)
.build();
Response response = null;
OkHttpClient client = new OkHttpClient();
try {
response = client.newCall(request).execute();
String json = response.body().string();
successCode = String.valueOf(response.code());
} catch (IOException e) {
e.printStackTrace();
}