gpt4 book ai didi

java - 上传文件到谷歌云端硬盘?

转载 作者:搜寻专家 更新时间:2023-11-01 08:21:21 24 4
gpt4 key购买 nike

我按照此文档将文件上传到 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 Drive SDK for Android。

首先,您需要通过选择设备的 Google 帐户登录 Google Drive。我猜你已经做到了,因为在 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 云端硬盘。

这里有一些 documentation .

希望这对您有所帮助。

关于java - 上传文件到谷歌云端硬盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50513817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com