【问题标题】:Download file from drive to upload into another drive using Google Drive API in Android Studio使用 Android Studio 中的 Google Drive API 从驱动器下载文件以上传到另一个驱动器
【发布时间】:2021-04-24 05:09:10
【问题描述】:

因此,在我的应用程序中,我需要从用户的驱动器中获取一个文件并将其上传到我的应用程序中。

我已经搜索过,似乎唯一的方法是从用户的驱动器下载文件,然后将其上传到我的。

我已经解决了所有 OAuth2 问题,并且能够创建、获取和列出文件。

但我的问题是,根据谷歌的文档,我应该将文件下载到声明为 OutputStream 的 ByteArrayOutputStream,但是当我上传文件时,我应该上传 java.io 文件

有谁知道我如何从下载方法中获取输出流并将其转换为文件以发送到上传方法?

这是我目前的代码:

下载文件的代码(我从谷歌得到的)

 public Task<java.io.File> downloadFile(PostFileHolder postFileHolder) {
        return Tasks.call(mExecutor, () -> {
            // Retrieve the metadata as a File object.

            Log.i("download file", "chegou");

            OutputStream outputStream = new ByteArrayOutputStream();
            mDriveService.files().export(postFileHolder.getGoogleId(), "application/pdf")
                    .executeMediaAndDownloadTo(outputStream);
          // I need to use the export method because I will have some docs and slides files

            return null;
        });
    }

这是上传文件的代码:

public Task<File> uploadFileWithMetadata(java.io.File javaFile, String mimeType, boolean isSlide,  @Nullable final String folderId) {
        return Tasks.call(mExecutor, () -> {

            Log.i("upload file", "chegou" );

            String convertTo;
            if(isSlide){
                convertTo = TYPE_GOOGLE_SLIDES;
            }
            else{
                convertTo = TYPE_GOOGLE_DOCS;
            }

            List<String> folder;
            if (folderId == null) {
                folder = Collections.singletonList("root");
            } else {

                folder = Collections.singletonList(folderId);
            }
            File metadata = new File()
            .setParents(folder)
            .setName(javaFile.getName())
            .setMimeType(convertTo);

            FileContent mediaContent = new FileContent(convertTo, javaFile);

            File uploadedFile = mDriveService.files().create(metadata, mediaContent)
                    .setFields("id,name,size,createdTime,modifiedTime,starred,thumbnailLink,mimeType")
                    .execute();

            Log.i("File ID: " , uploadedFile.getId());

            return uploadedFile;
        });
    }

谢谢!

【问题讨论】:

    标签: android android-studio google-drive-api google-drive-android-api


    【解决方案1】:

    您可以简单地使用以下最少的代码来创建文件。稍后使用此文件上传。

    val outputStream = ByteArrayOutputStream() // Your 
    val byteData = bos.toByteArray()
    
    
    val mainFile = File("Path with filename")
    //write the bytes in file
    val fos = FileOutputStream(mainFile)
    fos.write(bitmapdata)
    fos.flush()
    fos.close()
    

    由于你的 outputStream 被初始化为 ByteArrayOutputStream,所以你可以简单地使用 cast 方法,例如 ((ByteArrayOutputStream) outputStream).toByteArray()

    关于路径,您可以将文件保存在内部文件目录或外部存储中的任何位置作为临时文件。我建议您阅读 Data and File Storage Overviewthis 教程以获取更多信息。

    【讨论】:

    • 您好!感谢您的回答,但我无法将 outputStream 转换为 ByteArray,我认为是因为它被声明为 outputStream,如果它被定义为 ByteArrayOutputStream。另外,文件名的路径应该是什么?我应该将这个临时文件保存在哪里?
    • 更新了我的答案@Nina
    • 谢谢!我阅读了您推荐的页面,并且了解了内部文件目录的工作原理,但我仍然不知道在文件名中放入路径的确切内容。你介意给我一个例子,说明我应该在里面放什么吗?
    • @Nina stackoverflow.com/questions/3425906/… 看看这个,它会解决你的问题。解决后请点赞并接受答案。谢谢
    猜你喜欢
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多