java – 将文件上传到Google云端硬盘?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 将文件上传到Google云端硬盘?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3101字,纯文字阅读大概需要5分钟。
内容图文
![java – 将文件上传到Google云端硬盘?](/upload/InfoBanner/zyjiaocheng/697/3ecb5e254f564f74b4a104c50a3afa6b.jpg)
我按照此文档将文件上传到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)来实现,而不是使用Google Drive SDK for Android.
首先,您需要通过选择设备的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云端硬盘,而无需显示任何选择对话框,也无需用户互动.
这里你有一些documentation.
希望这可以帮助.
内容总结
以上是互联网集市为您收集整理的java – 将文件上传到Google云端硬盘?全部内容,希望文章能够帮你解决java – 将文件上传到Google云端硬盘?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。