The app folder in Drive is hidden. Your app can save app data, such as configuration files and archives, in this folder. When your app attempt to create a file in Drive, the app folder is automatically created. This folder stores all files with which the user should not have direct interactions. This folder can be accessed only by your app, and the content in the folder is hidden to the user and other apps using Drive. Your app can provide related functions for users to manually delete some data files or folders.
1. You have made all necessary preparations.
2. You have downloaded the client sample code and tried it out.
private Drive buildDrive() {
Drive service = new Drive.Builder(CredentialManager.getInstance().getCredential(), context).build();
return service;
} Call the Files.Create API to upload or back up a file to the app data folder.
To upload a file to the app data folder, you need to specify the app data root folder applicationData as the parent folder of the file in the Files.Create request.
private void doBackUpData() {
try {
Drive drive = buildDrive();
String BackFileName = "AppDataBackUpFileName.jpg";
String personalBackUpFolder = "AppName";
java.io.File fileObject = new java.io.File("/sdcard/" + BackFileName);
Map<String, String> appProperties = new HashMap<>();
appProperties.put("appProperties", "property");
File file = new File();
file.setFileName(personalBackUpFolder + System.currentTimeMillis())
.setMimeType("application/vnd.huawei-apps.folder")
.setAppSettings(appProperties)
.setParentFolder(Collections.singletonList("applicationData"));
File directoryCreated = drive.files().create(file).execute();
String mimeType = mimeType(fileObject);
File content = new File();
content.setFileName(fileObject.getName())
.setMimeType(mimeType)
.setParentFolder(Collections.singletonList(directoryCreated.getId()));
mBackupFile = drive.files().create(content, new FileContent(mimeType, fileObject)).setFields("*").execute();
} catch (IOException e) {
Logger.e(TAG, "BackUpData error: " + e.toString());
}
} Call the Files.List and Files.Get APIs to download or restore files.
private void doRestoreData() {
Drive drive = buildDrive();
String searchFileName = "AppDataBackUpFileName.jpg";
String containers = "applicationData";
String queryFile = "fileName = '" + searchFileName + "' and mimeType != 'application/vnd.huawei-apps.folder'";
try {
Drive.Files.List request = drive.files().list();
FileList files;
List<File> backupFiles = new ArrayList<>();
while (true) {
// Query a file.
files = request.setQueryParam(queryFile)
.setPageSize(10)
.setOrderBy("fileName")
.setFields("category,nextCursor,files/id,files/fileName,files/size")
.setContainers(containers).execute();
if (files == null || files.getFiles().size() > 0) {
break;
}
// Read all file information.
backupFiles.addAll(files.getFiles());
String nextCursor = files.getNextCursor();
if (!StringUtils.isNullOrEmpty(nextCursor)) {
request.setCursor(files.getNextCursor());
} else {
break;
}
}
// Download a file.
long size = mBackupFile.getSize();
Drive.Files.Get get = drive.files().get(mBackupFile.getId());
MediaHttpDownloader downloader = get.getMediaHttpDownloader();
// Check whether direct download is available.
boolean isDirectDownload = false;
if (size < DIRECT_DOWNLOAD_MAX_SIZE) {
isDirectDownload = true;
}
downloader.setContentRange(0, size - 1).setDirectDownloadEnabled(isDirectDownload);
String restoreFileName = "restoreFileName.jpg";
java.io.File f = new java.io.File("/sdcard/" + restoreFileName);
get.executeContentAndDownloadTo(new FileOutputStream(f));
} catch (IOException e) {
Logger.e(TAG, "RestoreData error: " + e.toString());
}
}