android - 强制 Android 的 Drive API 仅在在线模式下工作

标签 android google-play-services google-drive-android-api

最近,我刚刚设法将 Drive API 相关代码从 Google APIs Client Library for Java 迁移到 Google Play services client library

以下代码是在 appdata 文件夹中搜索一个文件,然后将其下载为临时文件。

private static GoogleCloudFile searchFromGoogleDrive(GoogleApiClient googleApiClient, HandleStatusable h, PublishProgressable p) {
    DriveFolder driveFolder = Drive.DriveApi.getAppFolder(googleApiClient);

    final String titleName = ("my-title");
    Query query = new Query.Builder()
            .addFilter(Filters.and(
                Filters.contains(SearchableField.TITLE, titleName),
                Filters.eq(SearchableField.TRASHED, false)
            ))
            .build();

    DriveApi.MetadataBufferResult metadataBufferResult = driveFolder.queryChildren(googleApiClient, query).await();

    if (metadataBufferResult == null) {
        return null;
    }

    Status status = metadataBufferResult.getStatus();

    if (!status.isSuccess()) {
        h.handleStatus(status);
        return null;
    }

    MetadataBuffer metadataBuffer = null;
    boolean needToReleaseMetadataBuffer = true;

    try {
        metadataBuffer = metadataBufferResult.getMetadataBuffer();
        if (metadataBuffer != null ) {
            long checksum = 0;
            long date = 0;
            int version = 0;
            Metadata metadata = null;

            for (Metadata md : metadataBuffer) {
                if (p.isCancelled()) {
                    return null;
                }

                if (md == null || !md.isDataValid()) {
                    continue;
                }

                final String title = md.getTitle();

                // ...

                metadata = md;

                break;

            }   // for

            if (metadata != null) {
                // Caller will be responsible to release the resource. If release too early,
                // metadata will not readable.
                needToReleaseMetadataBuffer = false;
                return GoogleCloudFile.newInstance(metadataBuffer, metadata, checksum, date, version);
            }
        }   // if
    } finally {
        if (needToReleaseMetadataBuffer) {
            if (metadataBuffer != null) {
                metadataBuffer.release();
            }
        }
    }

    return null;
}

public static CloudFile loadFromGoogleDrive(GoogleApiClient googleApiClient, HandleStatusable h, PublishProgressable p) {
    final java.io.File directory = ...

    GoogleCloudFile googleCloudFile = searchFromGoogleDrive(googleApiClient, h, p);

    if (googleCloudFile == null) {
        return null;
    }

    try {
        DriveFile driveFile = googleCloudFile.metadata.getDriveId().asDriveFile();
        DriveApi.DriveContentsResult driveContentsResult = driveFile.open(googleApiClient, DriveFile.MODE_READ_ONLY, null).await();

        if (driveContentsResult == null) {
            return null;
        }

        Status status = driveContentsResult.getStatus();
        if (!status.isSuccess()) {
            h.handleStatus(status);
            return null;
        }

        final long checksum = googleCloudFile.checksum;
        final long date = googleCloudFile.date;
        final int version = googleCloudFile.version;

        p.publishProgress(MyApplication.instance().getString(R.string.downloading));

        final DriveContents driveContents = driveContentsResult.getDriveContents();

        InputStream inputStream = null;
        java.io.File outputFile = null;
        OutputStream outputStream = null;

        try {
            inputStream = driveContents.getInputStream();
            outputFile = java.io.File.createTempFile("me", ".zip", directory);
            outputFile.deleteOnExit();
            outputStream = new FileOutputStream(outputFile);

            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }
        } catch (IOException ex) {
            Log.e(TAG, "", ex);
        } finally {
            org.yccheok.file.Utils.close(outputStream);
            org.yccheok.file.Utils.close(inputStream);
            driveContents.discard(googleApiClient);
        }

        if (outputFile == null) {
            return null;
        }

        return CloudFile.newInstance(outputFile, checksum, date, version);
    } finally {
        googleCloudFile.metadataBuffer.release();
    }
}

以前,当我使用 Java 版 Google API 客户端库时,如果没有互联网连接,类似的代码只会抛出异常。

但是,当我使用 Google Play 服务客户端库时,上面的代码仍然可以成功运行,即使我的设备处于飞行模式并关闭了 wifi。 em>

Google Play 服务客户端库 中的 Drive API 似乎能够在没有互联网连接时读取离线文件。

有没有办法,在没有互联网连接的情况下,执行上述代码会失败?因为,我想避免让我的用户下载过时的云文件的风险。

最佳答案

无法按照您的要求强制失败。

您可以做的是在查询之前请求与服务器同步:https://developers.google.com/android/reference/com/google/android/gms/drive/DriveApi.html#requestSync(com.google.android.gms.common.api.GoogleApiClient)

如果没有连接,同步将因网络错误而失败。

关于android - 强制 Android 的 Drive API 仅在在线模式下工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34735443/

相关文章:

android - 如何在 Android DatePicker 中设置自定义月份的名称?

android - 将 google-play-services 添加到 android 依赖项

android - Google 游戏登录 - 错误 12501 无效响应

android - 在未集成 Google 移动广告的情况下在 Android 上使用 Google Advertising Id

java - 如何将 "convert"任务 <> 发送到 google.api.services.drive.model.File?

android - 关于在 Android 中以编程方式备份​​ Google Drive 上的 Sqlite 文件和照片的示例项目

android - 当 WebView 无法使用互联网时,如何显示无互联网连接弹出窗口和 html 页面?

java - Android中读取json数组的方法

java - 是否可以在不同的 Activity 中使用多个 AsyncTask 将数据发布到服务器(Android)?

android - 卡在与 GoogleApiClient 的连接失败循环中