正在将文件上传到 Google 驱动器?
uploading a file to Google Drive?
我按照此文档将文件上传到 Google 驱动器:https://developers.google.com/drive/android/files
现在每次我想上传文件时,都会出现 Google Drive 的弹出窗口,询问我上传文件的位置和名称。我不想要那个弹出窗口,我想直接上传文件。
有什么方法可以做到这一点吗?我在文档中找不到任何内容。
我也发现了: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
}
可以使用 Google Drive REST API for Android 来实现(它直接使用 Google Drive API),而不是使用Google Android.
驱动 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();
}
}
请注意,我使用了完整的 class 名称,以便将 Java 文件与驱动器文件区分开来。
还要注意这个调用是同步的,所以必须在非UiThread中调用。
此方法能够创建文件并将其上传到 Google 驱动器,而无需显示任何选择器对话框且无需用户交互。
给你一些documentation。
希望对您有所帮助。
我按照此文档将文件上传到 Google 驱动器:https://developers.google.com/drive/android/files
现在每次我想上传文件时,都会出现 Google Drive 的弹出窗口,询问我上传文件的位置和名称。我不想要那个弹出窗口,我想直接上传文件。
有什么方法可以做到这一点吗?我在文档中找不到任何内容。
我也发现了: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
}
可以使用 Google Drive REST API for Android 来实现(它直接使用 Google Drive API),而不是使用Google Android.
驱动 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();
}
}
请注意,我使用了完整的 class 名称,以便将 Java 文件与驱动器文件区分开来。
还要注意这个调用是同步的,所以必须在非UiThread中调用。
此方法能够创建文件并将其上传到 Google 驱动器,而无需显示任何选择器对话框且无需用户交互。
给你一些documentation。
希望对您有所帮助。