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

是92:

我遵循以下文档以将文件上传到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
}
马克·埃斯特拉达(Marc Estrada):

可以使用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云端硬盘,而无需显示任何选择器对话框,也无需用户交互。

这里有一些文档

希望这可以帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将文件上传到Google云端硬盘中的特定文件夹

使用Android将文本文件上传到Google云端硬盘

通过Javascript API将多个文件上传到Google云端硬盘的最佳策略

使用javascript(无html表单)以编程方式将文件上传到Google云端硬盘

Google云端硬盘API将文件上传到Java团队硬盘

使用Windows服务自动将文件上传到Google云端硬盘文件流

将文件上传到Google云端硬盘后,如何处理该事件(调用脚本)?

使用gapi将文件上传到Google云端硬盘

C:通过HTTP请求将文件上传到Google云端硬盘的错误408响应

通过HTTP请求(C)将文件上传到Google云端硬盘

无法将文件上传到Google云端硬盘上的特定文件夹

使用Curl将文件上传到Google云端硬盘会显示401

将文件从网络上传到Google云端硬盘

使用PyDrive将图片上传到Google云端硬盘

pydrive:尝试将文件从远程服务器上传到Google云端硬盘

可靠地将大文件上传到Google云端硬盘

如何将字符串作为文件上传到Google云端硬盘?

无法将文件上传到Google云端硬盘中的文件夹

Angular Gapi将简单文本上传到Google云端硬盘

使用cURL将文件上传到Google云端硬盘-access_token过期问题

使用http上传到Google云端硬盘文件夹

将内存中的文件下载并上传到Google云端硬盘

将文件从Blobstore上传到Google云端硬盘

在后台将文件上传到Google云端硬盘

Python:如何使用folderId将文件上传到Google云端硬盘中的特定文件夹

使用意图将文件上传到Google云端硬盘

使用Dropzone将文件上传到Google云端硬盘

如何将持续更新的文件夹上传到Google云端硬盘?

使用php curl将文件上传到Google云端硬盘