如何从android将文件上传到谷歌驱动器 [英] How do I upload file to google drive from android

查看:13
本文介绍了如何从android将文件上传到谷歌驱动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了一天多的时间,但没有得到任何可行的解决方案,可以让我将文件上传/下载到 Google Drive.

I have spend more then one day but not getting any working solution which provide me uploading / downloading files to Google Drive.

我尝试过Google Play Service,但没有找到任何上传/下载文件的方法.

I have tried Google Play Service but i didn't find any method which upload / download files.

我尝试了谷歌客户端库,但有一些方法没有解决.

I try Google Client libraries but there are some method are not resolved.

如:

service.files().insert(body, mediaContent).execute();
errors: The method execute() is undefined for the type Drive.Files.Insert

我可以通过以下代码上传图片,但这是 Google Drive 文件上传器.我一次只能上传一个文件.

I can upload image through below code but this is Google Drive file up loader. I can only upload one only one file at a time.

mFile = new java.io.File(fileList.get(i));
                Log.i(TAG, "Creating new contents.");
                Drive.DriveApi.newContents(mGoogleApiClient).addResultCallback(
                        new OnNewContentsCallback() {

                            @Override
                            public void onNewContents(ContentsResult result) {

                                if (!result.getStatus().isSuccess()) {
                                    Log.i(TAG, "Failed to create new contents.");
                                    return;
                                }

                                Log.i(TAG, "New contents created.");

                                OutputStream outputStream = result
                                        .getContents().getOutputStream();

                                byte[] byteStream = new byte[(int) mFile
                                        .length()];
                                try {
                                    outputStream.write(byteStream);
                                } catch (IOException e1) {
                                    Log.i(TAG, "Unable to write file contents.");
                                }

                                MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                                        .setMimeType("image/jpeg")
                                        .setMimeType("text/html")
                                        .setTitle("Android Photo.png").build();
                                // Create an intent for the file chooser, and
                                // start it.
                                IntentSender intentSender = Drive.DriveApi
                                        .newCreateFileActivityBuilder()
                                        .setInitialMetadata(metadataChangeSet)
                                        .setInitialContents(
                                                result.getContents())
                                        .build(mGoogleApiClient);
                                try {
                                    mActivity.startIntentSenderForResult(
                                            intentSender, REQUEST_CODE_CREATOR,
                                            null, 0, 0, 0);
                                    publishProgress(1);
                                } catch (SendIntentException e) {
                                    Log.i(TAG, "Failed to launch file chooser.");
                                    publishProgress(0);
                                }
                            }
                        });

但仍在为下载文件而战.

But still fighting for downloading a file.

推荐答案

我得到了解决方案.我们绝不应该使用 Android API 来获得完整的云端硬盘访问权限.我们应该使用纯 java 代码,因为 Google 也说过要访问 Drive 以进行广泛访问,请使用 java 库.

I got the solution. We should never use Android API for complete Drive access. We should work on pure java code as Google also said that to access Drive for broad access use java libraries.

我删除了所有与 Google Play 服务相关的代码.我现在完全使用java,可以轻松上传、删除、编辑、下载所有我想要的东西.

I remove all the code related to Google play services. I am now using completely using java and easily upload, delete, edit, download all whatever I want.

还有一点,Google 文档没有提供关于 Google Drive 与 android api 相关的详细描述,而在使用 java 库时,您可以获得已经创建的方法等等.

One more thing Google doc doesn't provide a detail description about Google Drive in respective to android api while when work on java libraries you can get already created methods and more.

我没有提供任何代码,只是说我或其他对 Drive 完全访问感兴趣的人使用基于 Java 的代码.

I am not giving any code but saying that for me or for others who interested in Drive complete access use Java based codes.

这篇关于如何从android将文件上传到谷歌驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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