【问题标题】:Android app: upload file to google drive, share link, download fileAndroid应用:上传文件到谷歌驱动器,分享链接,下载文件
【发布时间】:2015-07-19 16:14:51
【问题描述】:

我的 Android 应用应该提供通过 google drive 共享文件的功能:

1) 上传一个文件(之前从 sd 卡中选择)到谷歌驱动器
2) 取回上传文件的链接(网址)
3) 分享此链接与该应用的其他用户
4) 其他用户可以下载共享文件到他们设备的 sd 卡

所有这些功能都应该在应用程序中可用,而无需使用浏览器。
有谁知道我如何实现步骤 1、2 和 4?
在此先感谢!
格哈德

【问题讨论】:

  • 第 3 点:所有用户都使用同一个 Android 应用,还是您考虑使用其他(可能是非 Android)应用? (读/写文件)
  • 嗨,seanpj!是的,所有用户都将使用相同的 Android 应用程序。这意味着:只能从安卓应用访问上传的文件。
  • 我假设所有用户都在不同的 Google 登录名(gmail 帐户)下访问云端硬盘,对吧?
  • 不,只会共享一个帐户(例如,我将拥有 1 TB 驱动器空间,以后可能会更多,我将为此支付月费),每个用户将上传到这个帐户/从这个帐户下载。这意味着,不需要登录驱动器帐户。

标签: android download upload google-drive-api google-drive-android-api


【解决方案1】:

这可以帮助您上传 Google Drive 文件 -

首先,进行身份验证

AccountManager am = AccountManager.get(activity);
  am.getAuthToken(am.getAccounts())[0],
    "oauth2:" + DriveScopes.DRIVE,
  new Bundle(),
  true,
  new OnTokenAcquired(),
  null);

现在需要设置令牌

     private class OnTokenAcquired implements AccountManagerCallback<Bundle> {
    @Override
    public void run(AccountManagerFuture<Bundle> result) {
        try {
            final String token = result.getResult().getString(AccountManager.KEY_AUTHTOKEN);
            HttpTransport httpTransport = new NetHttpTransport();
            JacksonFactory jsonFactory = new JacksonFactory();
            Drive.Builder b = new Drive.Builder(httpTransport, jsonFactory, null);
            b.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
                @Override
                public void initialize(JSonHttpRequest request) throws IOException {
                    DriveRequest driveRequest = (DriveRequest) request;
                    driveRequest.setPrettyPrint(true);
                    driveRequest.setKey(CLIENT ID YOU GOT WHEN SETTING UP THE CONSOLE BEFORE YOU STARTED CODING)
                    driveRequest.setOauthToken(token);
                }
            });

            final Drive drive = b.build();

            final com.google.api.services.drive.model.File body = new 
          com.google.api.services.drive.model.File();
            body.setTitle("My Test File");
          body.setDescription("A Test File");
           body.setMimeType("text/plain");

            final FileContent mediaContent = new FileContent("text/plain",  
            "Your data")
            new Thread(new Runnable() {
                public void run() {
                    try {
                        com.google.api.services.drive.model.File file =   
                 drive.files().insert(body, mediaContent).execute();
                        alreadyTriedAgain = false; 
                    } catch (IOException e) {
                        if (!alreadyTriedAgain) {
                            alreadyTriedAgain = true;
                            AccountManager am = AccountManager.get(activity);
                            am.invalidateAuthToken(am.getAccounts()[0].type, null); // Requires the permissions MANAGE_ACCOUNTS & USE_CREDENTIALS in the Manifest
                            am.getAuthToken (same as before...)
                        } else {
                            // Give up. Crash or log an error or whatever you want.
                        }
                    }
                }
            }).start();
            Intent launch = (Intent)result.getResult().get(AccountManager.KEY_INTENT);
            if (launch != null) {
                startActivityForResult(launch, 3025);
                return; // Not sure why... I wrote it here for some reason. Might not actually be necessary.
            }
        } catch (OperationCanceledException e) {
            // Handle it...
        } catch (AuthenticatorException e) {
            // Handle it...
        } catch (IOException e) {
            // Handle it...
        }
    }
}

现在,更新文件

    public void updateFile(Drive drive, File gFile, java.io.File jFile) throws   
    IOException {
        FileContent gContent = new FileContent("text/csv", jFile);
        gFile.setModifiedDate(new DateTime(false, jFile.lastModified(), 0));
        gFile = drive.files().update(gFile.getId(), gFile,   
        gContent).setSetModifiedDate(true).execute();
     }

另外,不要在 Manifest 中为

授予权限
GET_ACCOUNTS, USE_CREDENTIALS, MANAGE_ACCOUNTS, INTERNET WRITE_EXTERNAL_STORAGE 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    相关资源
    最近更新 更多