【问题标题】:File picker for uploading to Google Drive用于上传到 Google Drive 的文件选择器
【发布时间】:2018-06-18 02:53:29
【问题描述】:

我正在学习用于在 Android 上上传文件的 Google Api,并且确实找到了关于它的好示例。(您可以在此处查看该示例: https://github.com/sdivakarrajesh/Uploading-Files-to-Google-drive-using-java-client-api-in-Android/blob/master/app/src/main/java/com/dev/theblueorb/usingdrivejavaapi/DriveActivity.java) 但是,它只显示如何上传文件,而不是如何选择文件并将其上传到GG驱动器。下面是GG盘上传和创建文件夹的代码:

private void uploadFile() throws IOException {
        File fileMetadata = new File();;
        fileMetadata.setName("Sample File");
        fileMetadata.setMimeType("application/vnd.google-apps.spreadsheet");

        // For mime type of specific file visit Drive Doucumentation

        file2 = new java.io.File(path);
        InputStream inputStream = getResources().openRawResource(R.raw.template);
        try {
            FileUtils.copyInputStreamToFile(inputStream,file2);
        } catch (IOException e) {
            e.printStackTrace();
        }

        FileContent mediaContent = new FileContent("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",file2);


        File file = mService.files().create(fileMetadata, mediaContent)
                .setFields("id")
                .execute();


        Log.e(this.toString(),"File Created with ID:"+ file.getId());

        Toast.makeText(getApplicationContext(),
                "File created:"+file.getId() , Toast.LENGTH_SHORT).show();
    }


}




private void createFolderInDrive() throws IOException {
        File fileMetadata = new File();
        fileMetadata.setName("Sample Folder");
        fileMetadata.setMimeType("application/vnd.google-apps.folder");

        File file = mService.files().create(fileMetadata)
                .setFields("id")
                .execute();
        System.out.println("Folder ID: " + file.getId());

        Log.e(this.toString(),"Folder Created with ID:"+ file.getId());
        Toast.makeText(getApplicationContext(),
                "Folder created:"+file.getId() , Toast.LENGTH_SHORT).show();
    }

任何人都知道如何在设备上选择文件,然后将其上传到 GG 驱动器上的选定文件夹或示例中?

【问题讨论】:

    标签: java android google-api uploading filepicker


    【解决方案1】:

    Referencing these docs 对于基本文件上传,您应该能够通过在下面这一行中指定完整的文件路径为fileName 来“选择设备上的文件”:

    java.io.File fileContent = new java.io.File(filename);.

    例如,如果您在目录media 中有一个名为coolpic 的文件,您可以使用media/coolpic 作为filename。我参考的下一个文档强化了这一策略。路径将取决于根位置,您可以轻松调查。

    然后,查看this doc for working with folders in Google Drive。您需要找到文件夹 id 并在上传时使用

    fileMetadata.setParents(Collections.singletonList(folderId));

    请注意,您可以上传然后分两步移动,或者使用我上面的方法并设置上传时的文件夹。

    【讨论】:

    • 感谢您的回复。但是关于“选择文件”,我的意思是光标之类的东西,所以我浏览目录树来获取文件。有什么好的样本吗?谢谢!
    • @Matt Goodrich 感谢您的回复,您让我更接近我的需要。但是,如何在 Android 应用程序中创建一个 Picker,让用户使用 REST API 选择将文件保存到哪个文件夹?理想情况下,该选择器将返回用户选择的文件夹 ID,然后我将使用它来保存文件。提前致谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多