当前位置: 首页 > 面试题库 >

将文件上传到Google云端硬盘?

谷梁振
2023-03-14
问题内容

我遵循以下文档以将文件上传到Google云端硬盘:https
:
//developers.google.com/drive/android/files
现在,每次我要上传文件时,都会出现Google云端硬盘的弹出窗口,问我将其上传到哪里以及哪个名字。我不希望弹出窗口,我想直接上传文件。
有什么方法可以做到这一点?我找不到任何文档。我也发现了这一点:https : //developers.google.com/drive/api/v3/integrate-
create,但是我不知道是否可以使用它。你能给我一些提示吗?

编辑:
这是我的build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
        mavenCentral()
        maven { url 'https://maven.google.com' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
        maven { url 'https://maven.google.com' }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

问题答案:

可以使用Android版Google Drive REST API(直接使用Google Drive API)而不是Android版Google Drive
SDK来实现。

首先,您需要通过选择设备的Google帐户登录Google云端硬盘。我想您已经完成了,因为在Google Drive
SDK中还需要登录。要登录,我使用这种依赖性。

compile 'com.google.android.gms:play-services-auth:10.2.0'

然后,要上传文件(通过在REST API中创建文件),请执行以下操作。

首先,导入适用于Android的Google Drive REST API依赖项:

compile('com.google.api-client:google-api-client-android:1.22.0') {
    exclude group: 'org.apache.httpcomponents'
}
compile('com.google.apis:google-api-services-drive:v3-rev75-1.22.0') {
    exclude group: 'org.apache.httpcomponents'
}

其次,创建服务对象以与API连接:

private Drive createService(Context context) {
   GoogleAccountCredential mCredential = GoogleAccountCredential.usingOAuth2(context.getApplicationContext(), Arrays.asList(new String[]{DriveScopes.DRIVE})).setBackOff(new ExponentialBackOff());
   mCredential.setSelectedAccountName("your_logged_account_name");

   HttpTransport transport = AndroidHttp.newCompatibleTransport();
   JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
   mService = new com.google.api.services.drive.Drive.Builder(
        transport, jsonFactory, mCredential)
        .setApplicationName(context.getString(R.string.app_name))
        .build();

  return mService;
}

之后,当您登录并拥有Drive实例(服务)时,可以创建一个文件:

public void uploadFile(java.io.File fileContent) {
   try {
     com.google.api.services.drive.model.File file = new com.google.api.services.drive.model.File();
     file.setName("filen_ame");

     List<String> parents = new ArrayList<>(1);
     parents.add("parent_folder_id"); // Here you need to get the parent folder id
     file.setParents(parents);

     FileContent mediaContent = new FileContent("your_file_mime_type", fileContent);
     mService.files().create(file, mediaContent).setFields("id").execute();
     Log.d(TAG, "File uploaded");
   } catch (IOException e) {
     Log.e(TAG, "Error uploading file: ", e);
     e.printStackTrace();
   }
}

请注意,我使用完整的类名来区分Java文件和驱动器文件。

另请注意,此调用是同步的,因此必须在非UiThread中调用它。

这种方法可以创建文件并将其上传到Google云端硬盘,而无需显示任何选择器对话框,也无需用户交互。

这里有一些文档。

希望这可以帮助。



 类似资料:
  • 问题内容: 我正在尝试通过我的应用一次上传多个文件。它识别何时选择了2个或更多文件。但是,它只会将所选的第一个文件上传到我的驱动器。同样(尽管很小),我想知道如何将textarea字体更改为Times New Roman,以使其与其余字体保持一致。 代码 form.html 问题答案: 我知道这个问题很旧,但是找到它和相关的问题之后,我再也无法使多文件上传正常工作了。因此,在将我的头撞在墙上很多次

  • 问题内容: 我需要将某些文件类型从某些linux服务器备份到GDrive(不仅是可转换为GDocs格式的文件)。 用python脚本做到这一点的最简单,最优雅的方法是什么?与GDocs有关的任何解决方案都适用吗? 问题答案: 您可以使用Documents List API编写一个写入Drive的脚本: https://developers.google.com/google-apps/docume

  • 问题内容: 我已经使用官方API编写了一个应用程序,可以将其上传到Google云端硬盘,效果很好。但是我还是找不到取消上传的方法。我正在ASyncTask中运行它,到目前为止,我已经尝试过使驱动器对象,文件路径,令牌和其他一些变量都没有。我也尝试过使令牌无效,但是看来这只会将其从缓存中删除,而实际上并没有使服务器端无效。我在ASyncTask上调用了cancel,但是一旦上传开始就被取消了,似乎没

  • 问题内容: 我在Google云端硬盘中有一个文件夹想要嵌入到我的网站中。我找不到嵌入代码或Google云端硬盘帮助文章中记录的任何内容。 问题答案: Google云端硬盘文件夹可以嵌入并显示在和视图中: 列表显示 网格视图 问 :什么是文件夹ID(FOLDER-ID),如何获取? 答 :转到Google云端硬盘>>打开文件夹>>在浏览器地址栏中查看其URL。例如: 请注意需要许可的文件夹 此技术最

  • 问题内容: 我在Google云端硬盘上存储了一个zip文件(已公开共享)。我想知道如何在Golang中下载它。当前代码仅创建一个名为“ file.zip”的空白文件: 问题答案: 我找到了解决方案。使用:https://googledrive.com/host/ ID 而不是:https : //docs.google.com/uc?export=download& id = ID

  • 是否可以使用google drive api更改一个大文件,只上传更改的部分文件,而不是整个文件?我试图在API文档中找到关于它的信息,但只支持在连接重置后继续上传。它似乎是一个重要的特征。