I am trying to make the Android app managing photos. This app creates a zip file and uploads it to Google Drive for backup. To restore data, it downloads the zip file. But, It cannot download the file. Actual size of the zip file is 44MB, but size of downloaded file is 149bytes. How can I download a large file(44MB) by Google Drive REST API?
Actual size of the zip file is 44MB.
but, according to Android Monitor, size of the downloaded file is 149bytes.
CODE:
public class MainActivity extends AppCompatActivity {
private static final String[] SCOPES = {DriveScopes.DRIVE};
// same code as google drive rest api v3 quick start
// (https://developers.google.com/drive/v3/web/quickstart/android)
private class MakeRequestTask extends AsyncTask<Void, Void, List<String>> {
private Drive service = null;
private Exception lastError = null;
MakeRequestTask(GoogleAccountCredential credential) {
HttpTransport transport = AndroidHttp.newCompatibleTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
service = new Drive.Builder(transport, jsonFactory, credential).setApplicationName("Weight loss tracker with pictures").build();
}
@Override
protected List<String> doInBackground(Void... voids) {
try {
String pageToken = null;
int count = 0;
File target = null;
do {
FileList result = service.files().list()
.setFields("nextPageToken, files(id, name, createdTime, size)")
.setPageToken(pageToken)
.execute();
for (File file : result.getFiles()) {
String fileName = file.getName();
if (fileName != null && !"".equals(fileName) && fileName.startsWith("backup-")) {
target = file;
System.out.println("size1:" + file.getSize());
}
}
pageToken = result.getNextPageToken();
System.out.println("PAGETOKEN:" + pageToken);
} while (pageToken != null);
if (target == null) {
System.out.println("There is no target to restore.");
return null;
}
String id = target.getId();
System.out.println("DOWNLOAD TARGET ID:" + id);
java.io.File appDataPath = getActivity().getApplicationContext().getFilesDir();
String outputFileName = "restore.zip";
java.io.File restoreFile = new java.io.File(appDataPath, outputFileName);
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
FileOutputStream fos = new FileOutputStream(restoreFile)) {
service.files().get(id).executeAndDownloadTo(outputStream);
System.out.println("size2:" + outputStream.size());
outputStream.writeTo(fos);
}
System.out.println("size3:" + restoreFile.length());
BackupUtil.getInstance().restore(restoreFile, appDataPath);
} catch (UserRecoverableAuthIOException e) {
e.printStackTrace();
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
Andriod Monitor:
I/System.out: add:{"createdTime":"2018-02-20T15:19:56.345Z","id":"1g5-R8vblk9Wp_NLCygP04z-Zu1kpjkNq","name":"backup-2018-02-21-00-19-35-976.zip","size":"46523824"}
I/System.out: size1:46523824
I/System.out: PAGETOKEN:null
I/System.out: DOWNLOAD TARGET ID:1g5-R8vblk9Wp_NLCygP04z-Zu1kpjkNq
I/System.out: size2:149
I/System.out: size3:149
EDIT:
I have added following code to know the data of the file with 149 bytes.
String content = new String(outputStream.toByteArray());
System.out.println("content:" + content);
Android Monitor is following.
I/System.out: content:{
I/System.out: "kind": "drive#file",
I/System.out: "id": "1g5-R8vblk9Wp_NLCygP04z-Zu1kpjkNq",
I/System.out: "name": "backup-2018-02-21-00-19-35-976.zip",
I/System.out: "mimeType": "application/zip"
I/System.out: }