Java 将Google驱动器文件下载到Storage Access Framework指定的目录

Java 将Google驱动器文件下载到Storage Access Framework指定的目录,java,android,google-drive-api,storage,google-drive-android-api,Java,Android,Google Drive Api,Storage,Google Drive Android Api,我正在尝试使用ACTION_OPEN_DOCUMENT_TREE将Google驱动器文件下载到Storage Access Framework指定的目录 当手动提供路径时,我通过使用这个Runnable实现了这一点 private class DownloadFileThread implements Runnable { private String fileId; private String path; public DownloadFileThread(Stri

我正在尝试使用ACTION_OPEN_DOCUMENT_TREE将Google驱动器文件下载到Storage Access Framework指定的目录

当手动提供路径时,我通过使用这个Runnable实现了这一点

private class DownloadFileThread implements Runnable {
    private String fileId;
    private String path;

    public DownloadFileThread(String fileId, String path) {
        this.fileId = fileId;
        this.path = path;
    }

    @Override
    public void run() {
        if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
            PackageManager.PERMISSION_GRANTED) {

            try {
                String filename = driveService.files().get(fileId).execute().getName();

                // content
                OutputStream outputStream = new ByteArrayOutputStream();
                driveService.files().get(fileId)
                    .executeMediaAndDownloadTo(outputStream);
                String filecontent = outputStream.toString();

                // create file
                FileOutputStream fos = new FileOutputStream(path + filename);
                byte[] buffer = filecontent.getBytes();
                fos.write(buffer, 0, buffer.length);
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }


        } else {
            requestPermissions(activity, new String[] {
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            }, 1);
        }
    }
}    
我是这样开始的

public void downloadFileExplorer() {
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
    startActivityForResult(intent, REQUEST_CODE_EXPLORER_DOWNLOAD_FILE);
}
else if (requestCode == REQUEST_CODE_EXPLORER_UPLOAD_FILE) {
    DriveExplorer driveExplorer = new DriveExplorer(driveService, this);
    FileUtils fileUtils = new FileUtils(this);
    String path = fileUtils.getPath(uri);
    driveExplorer.uploadFile(path);
}
并在onActivityResult()中处理它

启动Runnable

 public void downloadFile(String fileId, String path) {
     new Thread(new DownloadFileThread(fileId, path)).start();
 }

我可以像这样上传一个选定的文件

public void downloadFileExplorer() {
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
    startActivityForResult(intent, REQUEST_CODE_EXPLORER_DOWNLOAD_FILE);
}
else if (requestCode == REQUEST_CODE_EXPLORER_UPLOAD_FILE) {
    DriveExplorer driveExplorer = new DriveExplorer(driveService, this);
    FileUtils fileUtils = new FileUtils(this);
    String path = fileUtils.getPath(uri);
    driveExplorer.uploadFile(path);
}
这是可行的,因为URI引用了完整路径+文件名,我在网上找到的实用类可以提取路径,例如:/storage/self/primary/Download/abc.txt


然而,当下载时,URI只引用文件夹路径,实用程序无法处理这个问题。
例如:/storage/self/primary/Download/

使用
DocumentFile.fromtreeeuri()
为用户选择的树创建
DocumentFile
。使用
DocumentFile
上的
createFile()
创建一个
DocumentFile
来表示要创建的内容。在新的
DocumentFile
上调用
getUri()
,为要创建的文档获取
Uri
,并使用
ContentResolver
openOutputStream()
为该
Uri
获取
OutputStream
。将内容写入流,然后刷新并关闭流。@commonware非常感谢!使用
DocumentFile.fromtreeeuri()
为用户选择的树创建
DocumentFile
。使用
DocumentFile
上的
createFile()
创建一个
DocumentFile
来表示要创建的内容。在新的
DocumentFile
上调用
getUri()
,为要创建的文档获取
Uri
,并使用
ContentResolver
openOutputStream()
为该
Uri
获取
OutputStream
。将内容写入流,然后刷新并关闭流。@commonware非常感谢!