【问题标题】:File not downloading using Google Drive Rest API V3未使用 Google Drive Rest API V3 下载文件
【发布时间】:2020-03-07 16:47:16
【问题描述】:

我已将 sqlite 文件上传到谷歌驱动器以进行备份。上传工作正常。但下载代码不起作用。我想下载这个以将此数据库文件重新导入我的应用程序。我正在关注 Google Drive Rest Api v3 官方文档并从中复制代码,但不起作用。

我已经在 oAuth 同意屏幕中提供了所有类型的范围,并从开发者控制台启用了 Google Drive Api,这就是上传文件正常工作的原因。

代码:

  public Task<Void> downloadFile(String fileId) {
        return Tasks.call(mExecutor, () -> {
            OutputStream outputStream = new FileOutputStream(Environment.getExternalStorageDirectory() + "/" + java.io.File.separator + "test.db");

            mDriveService.files().get(fileId).executeMediaAndDownloadTo(outputStream);
            return null;
        });

    }

我缺少什么以及如何解决这个问题?

注意:我已经记录了文件 ID,它提供了正确的文件 ID。即文件ID:1NeUxYzCeyo_YZCqkLnOOmD2kP5PBLnxq

【问题讨论】:

标签: android google-drive-api


【解决方案1】:

我也有这样的问题,我所做的是使用

files().list

用于查找我的文件

public Task<Void> downloadFile(String fileId){
        OutputStream outputStream = new 
            FileOutputStream(Environment.getExternalStorageDirectory() + "/" + 
            java.io.File.separator + "test.db");
        return Tasks.call(executor, new Callable<File>() {
            @Override
            public File call() throws Exception {
                FileList list;
                File file=null;
                list=driveService.files().list().execute();
                if (list!=null&&!list.isEmpty()) {
                    for (File f : list.getFiles()) {
                        if(f.getId()==fileId)
                            file = f;
                    }
                    if (file!=null)                
                        mDriveService.files().get(file.getId())
                        .executeMediaAndDownloadTo(outputStream);    
                }    
            }
        });
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-15
    • 2021-10-05
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 2020-05-23
    • 2023-02-06
    • 2020-02-05
    相关资源
    最近更新 更多