DriveFolder.listChildren()无法显示其所有的孩子 [英] DriveFolder.listChildren() not showing all its children

查看:83
本文介绍了DriveFolder.listChildren()无法显示其所有的孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个应用程序与谷歌驱动器的工作原理。

I am making an app which works with google drive.

我需要得到一个文件夹内的所有文件的列表,但似乎它不是可能的:当我调用listFiles()方法,我不能让一个DriveFolder内的所有文件。不仅如此,在文件中我得到的名单中,有我删除了previously一些文件。

I need to get a list of ALL files inside a folder, but it seems that it isnt possible: when I call listFiles method() i cant get all files inside a DriveFolder. But not only that, in the list of files I get, there are some files which I deleted previously.

我已阅读,它可以通过,因为谷歌播放服务同步延迟造成的,但我已经选择了立即同步在我的帐户设置选项,所以我觉得这个问题是不是由延迟造成的。

I have read that it may be caused by a sync delay because of google play services, but I had selected 'Sync now' option in my account settings so I think that the problem is not caused by that delay.

这是方法我讲的<一个href="http://developer.android.com/reference/com/google/android/gms/drive/DriveFolder.html#listChildren(com.google.android.gms.common.api.GoogleApiClient)" rel="nofollow">http://developer.android.com/reference/com/google/android/gms/drive/DriveFolder.html#listChildren(com.google.android.gms.common.api.GoogleApiClient)

这是code我写了:

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它,我收到了MetadataBuffer与计数0。

Obviously, I have created a folder called 'Neewie' and inside it a file. I have got a DriveFolder pointing to Neewie folder, but when listChildren it, I receive a MetadataBuffer with count 0.

我需要做一些事情列出所有文件? 什么毛病克库?

Do I need to do something to list ALL files? Is something wrong with gms library?

推荐答案

这使我很长的时间来重新运行你的例子,但我可能有几个点,让你领先。

It would take me long time to re-run your example, but I may have a few points to get you ahead.

首先,因为在谷歌推动Android API没有删除功能还没有,你可能指的是在网络驱动器界面中的删除操作。状态Metadata.isTrashed()不正确,与这里讨论反映情况。而且采用的 requestSync()的没有帮助。但我让这个问题去,因为我希望他们有删除的GDAA实施修复。更多关于DELETE可以发现<一href="http://stackoverflow.com/questions/22376380/how-can-i-delete-google-drive-file-on-android-app/22405833#22405833">here.

First, since there is no DELETE functionality in Google Drive Android API yet, you are probably referring to the "remove" action in the web Drive interface. The status Metadata.isTrashed() does not reflect the status correctly as discussed here. And using requestSync() did not help. But I let this issue go since I expect them to fix it with the implementation of DELETE in GDAA. More on DELETE can be found here.

二,我用的是等待的版本要求在我的测试环境(包裹在AsyncTask的)来测试的东西。这是很容易步。我曾经历过一次,而一些怪异的行为,但不能精确定位它。由<一个固定它href="http://stackoverflow.com/questions/22382099/files-created-by-the-new-google-drive-android-api-gdaa-not-showing-up">clearing在谷歌播放服务缓存。

Second, I use the "await" version of the calls in my test environment (wrapped in AsyncTask) to test things. It is much easier to step through. I have experienced some "weird" behavior once a while, but could not pinpoint it. Fixed it by clearing the Google Play Services cache.

三,还有一个谷歌API客户包装类的 GooApiClnt 在这个code(在底部),这似乎产生稳定的结果 - 你可以尝试一下。你可以测试的findAll(即DriveApi.query)版本,而不是'listAll(即listChildren),因为它可以让你过滤TRASHED状态的前期。顺便说一句,我注意到,你没有登录你的榜样的Metadata.isTrashed()状态。这会给哪些文件被删除更好的视野。但同样,也只是确认同步延迟问题。

Third, there is a Google Api Client wrapper class GooApiClnt in this code (at the bottom) that seems to produce stable results - you may try it. You may test the 'findAll'(i.e. DriveApi.query) versions instead of 'listAll'(i.e. listChildren) since it allows you to filter the TRASHED state upfront. BTW, I noticed that you're not logging the Metadata.isTrashed() status in your example. It would give a better view of what files were deleted. But again, it would only confirm the sync latency issues.

最后,小问题 - 我不知道,如果它不是固定在已经GDAA水平 - 的的getMetadataBuffer释放/关闭。这是造成资源泄露在我的code。再次,见上面提到的Github上code,grep的mdb.close()'。

The last, minor issue - and I don't know if it wasn't fixed on the GDAA level already - the release/closing of the of the 'getMetadataBuffer'. It was causing resource leaks in my code. Again, see the Github code referenced above, grep 'mdb.close()'.

编码快乐,万事如意

这篇关于DriveFolder.listChildren()无法显示其所有的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆