android - Howto get GoogleDrive Quota(remaining available size) with GoogleApiClient? -
i use googleapiclient on android app. can not googledrive quota usage(total/available/etc..). googling..., may driveservice(com.google.api.services.drive.drive) possible... not use that......
how googledrive quota info?
(note) here googleapiclient builder.
gdapi = new googleapiclient.builder(mcontext) .addapi(plus.api) .addscope(plus.scope_plus_profile) .addapi(drive.api) .addscope(drive.scope_file) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .setaccountname(token) .build();
unfortunately (afaik), there no equivalent of getquotabytestotal(), getquotabytesused()
in gdaa.
so, way info (and other functionalities not present - thumbnails
, ...), add rest api. still, sure use mix carefully, may run many latency / timing issues.
there few pre-requisites accomplishing this:
1/ make sure include following jars in project:
compile 'com.google.apis:google-api-services-drive:v2-rev105-1.17.0-rc' compile 'com.google.http-client:google-http-client-gson:1.20.0' compile 'com.google.api-client:google-api-client-android:1.20.0' ... import com.google.api.client.extensions.android.http.androidhttp; import com.google.api.client.googleapis.extensions.android.gms.auth.googleaccountcredential; import com.google.api.client.json.gson.gsonfactory;
2/ need additional permission in manifest:
<uses-permission android:name="android.permission.get_accounts" />
3/ have com.google.api.services.drive.drive
service , query after gdaa's onconnected()
callback, so:
private static googleapiclient mgac; private static com.google.api.services.drive.drive mgoosvc; static boolean init(activity context) { if (context != null) try { mgoosvc = new com.google.api.services.drive.drive.builder( androidhttp.newcompatibletransport(), new gsonfactory(), googleaccountcredential.usingoauth2( context.getapplicationcontext(), collections.singletonlist(com.google.api.services.drive.drivescopes.drive_file) ) ).build(); mgac = new googleapiclient.builder(context) .addapi(drive.api) // ... additional apis .addscope(drive.scope_file) .addconnectioncallbacks(new googleapiclient.connectioncallbacks() { @override public void onconnectionsuspended(int i) { } @override public void onconnected(bundle bundle) { new thread(new runnable() { @override public void run() { try { com.google.api.services.drive.model.about = mgoosvc.about().get().execute(); system.out.println("total quota (bytes): " + about.getquotabytestotal()); system.out.println("used quota (bytes): " + about.getquotabytesused()); } catch (exception e) { e.printstacktrace(); } //} catch (userrecoverableauthioexception uraioex) { // // standard authorization failure - user fixable //} catch (googleauthioexception gaioex) { // // packagename /sha1 mismatch in devconsole //} catch (ioexception e) { // if (e instanceof googlejsonresponseexception) { // if (404 == ((googlejsonresponseexception) e).getstatuscode()) { // // '404 not found' in file scope, consider connected // } // } //} catch (exception e) { // // "the name must not empty" indicate // // unregistered / empty account in 'setselectedaccountname()' above //} } }).start(); } }) .addonconnectionfailedlistener(new googleapiclient.onconnectionfailedlistener() { @override public void onconnectionfailed(@nonnull connectionresult rslt) { // perform standard authorization dance } }) .build(); mgac.connect(); return true; } catch (exception e) {e.printstacktrace();} return false; }
notice, commented out error / authorization handling rest's execute()
method. because gdaa's connect()/onconnected()/onconnectionfailed()
handles authorization (if scopes identical).
good luck
Comments
Post a Comment