DriveFolder.listChildren() 未显示其所有子项

2023-12-04

我正在制作一个与谷歌驱动器一起使用的应用程序。

我需要获取文件夹内所有文件的列表,但似乎不可能:当我调用 listFiles 方法()时,我无法获取 DriveFolder 内的所有文件。但不仅如此,在我得到的文件列表中,还有一些我之前删除的文件。

我读到这可能是由于谷歌播放服务导致的同步延迟引起的,但我在帐户设置中选择了“立即同步”选项,所以我认为问题不是由该延迟引起的。

这就是我所说的方法http://developer.android.com/reference/com/google/android/gms/drive/DriveFolder.html#listChildren(com.google.android.gms.common.api.GoogleApiClient)

这是我写的代码:

public class ServicePull extends IntentService implements
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener{

private GoogleApiClient mApiClient;

public static void startPull(Context context) {
    Intent intent = new Intent(context, ServicePull.class);
    context.startService(intent);
}

public ServicePull() {
    super("ServicePull");
}

@Override
protected void onHandleIntent(Intent intent) {
    mApiClient = new GoogleApiClient.Builder(this)
            .addApi(Drive.API)
            .addScope(Drive.SCOPE_FILE)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    mApiClient.connect();
}



@Override
public void onConnected(Bundle bundle) {
    NotificationStatus.notify(this, "On Connected", "mApiClient Connected");
    DriveFolder driveFolder = Drive.DriveApi.getRootFolder(mApiClient);
    driveFolder.listChildren(mApiClient).setResultCallback(rootFolderCallback);
}

@Override
public void onConnectionSuspended(int i) {
    NotificationStatus.notify(this, "On Suspended", "mApiClient Suspended");
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    NotificationStatus.notify(this, "On failed", "mApiClient failed");
}

final private ResultCallback<DriveApi.MetadataBufferResult> rootFolderCallback =
        new ResultCallback<DriveApi.MetadataBufferResult>() {
            @Override
            public void onResult(DriveApi.MetadataBufferResult metadataBufferResult) {
                log("got root folder");
                MetadataBuffer buffer = metadataBufferResult.getMetadataBuffer();
                log("Buffer count  " + buffer.getCount());
                for(Metadata m : buffer){
                    log("Metadata name  " + m.getTitle() + "(" + (m.isFolder() ? "folder" : "file") + ")");
                    if (m.isFolder() && m.getTitle().equals("Neewie"))
                        Drive.DriveApi.getFolder(mApiClient, m.getDriveId())
                                .listChildren(mApiClient)
                                .setResultCallback(fileCallback);
                }
            }
};

final private ResultCallback<DriveApi.MetadataBufferResult> fileCallback =
        new ResultCallback<DriveApi.MetadataBufferResult>() {
            @Override
            public void onResult(DriveApi.MetadataBufferResult metadataBufferResult) {
                log("got file children");
                MetadataBuffer buffer = metadataBufferResult.getMetadataBuffer();
                //for(Metadata m : buffer){
                log("Buffer count  " + buffer.getCount());
                for(int i =0;i<buffer.getCount();i++){
                    Metadata m = buffer.get(i);
                    log(m.toString());
                    Drive.DriveApi.getFile(mApiClient, m.getDriveId())
                            .openContents(mApiClient, DriveFile.MODE_READ_ONLY,
                                    new DriveFile.DownloadProgressListener() {
                                        @Override
                                        public void onProgress(long bytesDownloaded, long bytesExpected) {
                                            // Update progress dialog with the latest progress.
                                            int progress = (int) (bytesDownloaded * 100 / bytesExpected);
                                            Log.wtf("TAG", String.format("Loading progress: %d percent", progress));
                                        }
                                    }
                            )
                            .setResultCallback(contentsCallback);
                }
            }
};


final private ResultCallback<DriveApi.ContentsResult> contentsCallback =
        new ResultCallback<DriveApi.ContentsResult>() {
            @Override
            public void onResult(DriveApi.ContentsResult contentsResult) {
                log("got file contents");
                File file = new File("storage/emulated/0/Downtests/tessing.txt");
                file.mkdirs();
                try {
                    InputStream input = contentsResult.getContents().getInputStream();
                    OutputStream output = new FileOutputStream(file);
                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = input.read(buf)) > 0) {
                        output.write(buf, 0, len);
                    }
                    input.close();
                    output.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        };

private void log(String s){
    Log.wtf("ServicePull", s);
}



}

显然,我创建了一个名为“Neewie”的文件夹,其中包含一个文件。我有一个 DriveFolder 指向 Neewie 文件夹,但是当 listChildren 时,我收到一个计数为 0 的 MetadataBuffer。

我需要做些什么来列出所有文件吗? gms库有问题吗?


我需要很长时间才能重新运行您的示例,但我可能有几点可以帮助您取得领先。

首先,由于 Google Drive Android API 中尚无删除功能,因此您可能指的是 Web Drive 界面中的“删除”操作。状态 Metadata.isTrashed() 未正确反映状态,如下所示在这里讨论。并使用请求同步()没有帮助。但我放弃了这个问题,因为我希望他们通过在 GDAA 中实现 DELETE 来解决它。有关 DELETE 的更多信息,请参见here.

其次,我在测试环境(包装在 AsyncTask 中)中使用调用的“await”版本来测试。一步步走过来就容易多了。我曾经经历过一些“奇怪”的行为,但无法具体指出。通过修复它清除 Google Play 服务缓存.

第三,有一个Google Api 客户端包装类GooAPI客户端在此代码中(在底部)似乎会产生稳定的结果 - 您可以尝试一下。您可以测试“findAll”(即 DriveApi.query)版本而不是“listAll”(即 listChildren)版本,因为它允许您预先过滤 TRASHED 状态。顺便说一句,我注意到您没有在示例中记录 Metadata.isTrashed() 状态。它可以更好地查看哪些文件被删除。但同样,它只会确认同步延迟问题。

最后一个小问题 - 我不知道它是否尚未在 GDAA 级别上修复 - “getMetadataBuffer”的释放/关闭。它导致我的代码中的资源泄漏。再次参见上面引用的 Github 代码 grep 'mdb.close()'。

快乐编码,祝你好运

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

DriveFolder.listChildren() 未显示其所有子项 的相关文章

随机推荐