Android应用:上传文件到谷歌驱动器,共享链接,下载文件

时间:2015-05-08 16:54:24

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

我的Android应用应该提供通过谷歌驱动器共享文件的功能:

1)上传一个文件(之前从SD卡中选择)到谷歌驱动器
2)获取链接(url)到上传的文件
3)与应用的其他用户分享此链接 4)其他用户可以下载共享文件到他们设备的SD卡
所有这些功能都应该在应用程序中提供,而无需使用浏览器 有没有人知道如何实施步骤1,2和4? 提前谢谢!
格哈德

1 个答案:

答案 0 :(得分:0)

这可以帮助您上传Google云端硬盘文件 -

首先,进行身份验证

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