将文件上传到Google云端硬盘? [英] uploading a file to Google Drive?

查看:184
本文介绍了将文件上传到Google云端硬盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


这是我的build.gradle:

 //顶级构建文件,您可以在其中添加所有子项目/模块共有的配置选项.buildscript {储存库{谷歌()jcenter()mavenCentral()maven {url'https://maven.google.com'}}依赖项{classpath'com.android.tools.build:gradle:3.1.2'//注意:请勿在此处放置应用程序依赖项;他们属于//在各个模块的build.gradle文件中}}所有项目{储存库{谷歌()jcenter()mavenCentral()maven {url'https://maven.google.com'}}}任务清理(类型:删除){删除rootProject.buildDir} 

解决方案

可以使用Android版Google Drive REST API(直接使用Google Drive API)来实现,而不必使用Android版Google Drive SDK.>

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

 编译'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'){排除组:"org.apache.httpcomponents"}编译('com.google.apis:google-api-services-drive:v3-rev75-1.22.0'){排除组:"org.apache.httpcomponents"} 

第二,创建服务对象以与API连接:

 私有驱动器createService(上下文上下文){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 =新的com.google.api.services.drive.Drive.Builder(传输,jsonFactory,mCredential).setApplicationName(context.getString(R.string.app_name)).建造();返回mService;} 

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

  public void uploadFile(java.io.File fileContent){尝试 {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");//在这里您需要获取父文件夹IDfile.setParents(parents);FileContent mediaContent = new FileContent("your_file_mime_type",fileContent);mService.files().create(file,mediaContent).setFields("id").execute();Log.d(TAG,文件已上传");} catch(IOException e){Log.e(TAG,上传文件时出错:",e);e.printStackTrace();}} 

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

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

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

这里有一些文档.

希望这会有所帮助.

I followed this documentation in order to upload files to Google Drive: https://developers.google.com/drive/android/files
Now every time I want to upload a file a popup of Google Drive appears, asking me where to upload it and with which name. I don't want that popup, I want to upload directly the file.
Is there any method in order to do that? I can't find anything into the docs. I found that too: https://developers.google.com/drive/api/v3/integrate-create, but I don't know if I can use it. Can you give me some hints?

EDIT:
This is my 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
}

解决方案

It can be achieved using the Google Drive REST API for Android (it uses the Google Drive API directly), instead of using Google Drive SDK for Android.

First of all, you need to login in Google Drive by choosing a Google Account of the device. I guess you've done it because in Google Drive SDK also needs to login. To login I use this dependence.

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

Then, to upload a file (by creating it in the REST API) do the following.

First, import the Google Drive REST API dependencies for Android:

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'
}

Second, create the service object in order to connect with the 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;
}

Afterwards, when you are logged in and you have the Drive instance (service), you can create a file:

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();
   }
}

Please note I used the full class name in order to differentiate the Java File to the Drive File.

Also note that this call is synchronous, so you must call it in a non-UiThread.

This method is able to create and upload a file to Google Drive without showing any chooser dialog and without user interaction.

Here you have some documentation.

Hope this helps.

这篇关于将文件上传到Google云端硬盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆