目录

Storing App Data

Use Cases

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.

Prerequisites

1. You have made all necessary preparations.

2. You have downloaded the client sample code and tried it out.

Creating a Drive Object

Call the Drive.Builder.build API to create a Drive object.
private Drive buildDrive() {  
    Drive service = new Drive.Builder(CredentialManager.getInstance().getCredential(), context).build();  
    return service;  
}

Uploading or Backing Up a File to the App Data Folder

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());
    }
}

Downloading or Restoring Files

Call the Files.List and Files.Get APIs to download or restore files.

In the Files.List request, set Containers to applicationData to read all files that have been uploaded to the app data folder. Then, the Files.Get API is called to obtain the file metadata in the app data folder.
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());
    }
}