android - Cannot get acces to file in google drive (Id must be provided) -
i making code based on android google drive demo. think i've copied setings propeerly can create files , folder , make queries; when tring doenload file recieve error saing id must provided.
how code works:
@ firs making query id's
of files name interested. , following:
04-21 11:36:51.374: d/folder id(6468): driveid:caeshdbcdzhlowswmlprblvnrxn4vud0nk5iqxphrmsyhgggmnuw1qxr 04-21 11:36:51.374: d/folder name(6468): gdgatheredtext.txt 04-21 11:36:51.374: d/folder id(6468): driveid:caeshdbcdzhlowswmlprblvkamr1vfhwngntcg9nbgsy_gcgmnuw1qxr 04-21 11:36:51.374: d/folder name(6468): gdgatheredtext.txt 04-21 11:36:51.374: d/folder id(6468): driveid:caeshdbcdzhlowswmlprblvlwe5swm10t1rxwxhzaley9acgmnuw1qxr 04-21 11:36:51.374: d/folder name(6468): gdgatheredtext.txt
i take id of last file caeshdbcdzhlowswmlprblvlwe5swm10t1rxwxhzaley9acgmnuw1qxr
, pass uumodefied code apidemo. error fierd @ line drivefile file = drive.driveapi.getfile(getgoogleapiclient(), params[0]);
load class
import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import android.content.context; import android.os.bundle; import android.util.log; import com.google.android.gms.common.api.resultcallback; import com.google.android.gms.drive.drive; import com.google.android.gms.drive.driveapi.contentsresult; import com.google.android.gms.drive.driveapi.driveidresult; import com.google.android.gms.drive.driveapi.metadatabufferresult; import com.google.android.gms.drive.drivefile; import com.google.android.gms.drive.driveid; import com.google.android.gms.drive.metadatabuffer; import com.google.android.gms.drive.query.filters; import com.google.android.gms.drive.query.query; import com.google.android.gms.drive.query.searchablefield; public class gdriveloadtext extends basegdriveactivity { private string id; @override protected void oncreate(bundle b) { super.oncreate(b); } @override public void onconnected(bundle connectionhint) { super.onconnected(connectionhint); query query = new query.builder() .addfilter(filters.eq(searchablefield.mime_type, "plain/text")) .addfilter( filters.eq(searchablefield.title, "gdgatheredtext.txt")) .build(); drive.driveapi.query(getgoogleapiclient(), query).setresultcallback( metadatacallback); } final private resultcallback<metadatabufferresult> metadatacallback = new resultcallback<metadatabufferresult>() { @override public void onresult(metadatabufferresult result) { if (!result.getstatus().issuccess()) { showmessage("problem while retrieving results"); finish(); } metadatabuffer metadata = result.getmetadatabuffer(); (int = 0; < metadata.getcount(); i++) { log.d("folder id", metadata.get(i).getdriveid().tostring()); log.d("folder name", metadata.get(i).gettitle().tostring()); } if (metadata.getcount() > 0) { string driveid = metadata.get(metadata.getcount() - 1) .getdriveid().tostring(); id =driveid.replace("driveid:", ""); log.d("file id", id); } metadata.close(); drive.driveapi.fetchdriveid(getgoogleapiclient(), id) .setresultcallback(idcallback); } }; final private resultcallback<driveidresult> idcallback = new resultcallback<driveidresult>() { @override public void onresult(driveidresult result) { new retrievedrivefilecontentsasynctask( gdriveloadtext.this).execute(result.getdriveid()); } }; final private class retrievedrivefilecontentsasynctask extends apiclientasynctask<driveid, boolean, string> { public retrievedrivefilecontentsasynctask(context context) { super(context); } @override protected string doinbackgroundconnected(driveid... params) { string contents = null; drivefile file = drive.driveapi.getfile(getgoogleapiclient(), params[0]); contentsresult contentsresult = file.opencontents( getgoogleapiclient(), drivefile.mode_read_only, null) .await(); if (!contentsresult.getstatus().issuccess()) { return null; } bufferedreader reader = new bufferedreader(new inputstreamreader( contentsresult.getcontents().getinputstream())); stringbuilder builder = new stringbuilder(); string line; try { while ((line = reader.readline()) != null) { builder.append(line); } contents = builder.tostring(); } catch (ioexception e) { log.e("download text", "ioexception while reading stream", e); } file.discardcontents(getgoogleapiclient(), contentsresult.getcontents()).await(); gathereddata.instance.setgatheredtext(contents); return contents; } @override protected void onpostexecute(string result) { super.onpostexecute(result); if (result == null) { showmessage("error while reading file"); return; } showmessage("file contents: " + result); finish(); } } }
base class
import android.app.activity; import android.content.intent; import android.content.intentsender.sendintentexception; import android.os.bundle; import android.util.log; import android.widget.toast; import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.googleplayservicesutil; import com.google.android.gms.common.api.googleapiclient; import com.google.android.gms.drive.drive; /** * abstract activity handles authorization , connection drive * services. */ public abstract class basegdriveactivity extends activity implements googleapiclient.connectioncallbacks, googleapiclient.onconnectionfailedlistener { private static final string tag = "basedriveactivity"; /** * account name. */ protected static final string extra_account_name = "account_name"; /** * request code auto google play services error resolution. */ protected static final int request_code_resolution = 1; /** * next available request code. */ protected static final int next_available_request_code = 2; /** * google api client. */ private googleapiclient mgoogleapiclient; /** * called when activity gets visible. connection drive services need * initiated activity visible. registers * {@code connectioncallbacks} , {@code onconnectionfailedlistener} on * activities itself. */ @override protected void onresume() { super.onresume(); if (mgoogleapiclient == null) { mgoogleapiclient = new googleapiclient.builder(this) .addapi(drive.api) .addscope(drive.scope_file) .addscope(drive.scope_appfolder) // required app folder sample .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .build(); } mgoogleapiclient.connect(); } /** * handles resolution callbacks. */ @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == request_code_resolution && resultcode == result_ok) { mgoogleapiclient.connect(); } } /** * called when activity gets invisible. connection drive service needs * disconnected activity invisible. */ @override protected void onpause() { if (mgoogleapiclient != null) { mgoogleapiclient.disconnect(); } super.onpause(); } /** * called when {@code mgoogleapiclient} connected. */ @override public void onconnected(bundle connectionhint) { log.i(tag, "googleapiclient connected"); } /** * called when {@code mgoogleapiclient} disconnected. */ @override public void onconnectionsuspended(int cause) { log.i(tag, "googleapiclient connection suspended"); } /** * called when {@code mgoogleapiclient} trying connect failed. * handle {@code result.getresolution()} if there resolution * available. */ @override public void onconnectionfailed(connectionresult result) { log.i(tag, "googleapiclient connection failed: " + result.tostring()); if (!result.hasresolution()) { // show localized error dialog. googleplayservicesutil.geterrordialog(result.geterrorcode(), this, 0).show(); return; } try { result.startresolutionforresult(this, request_code_resolution); } catch (sendintentexception e) { log.e(tag, "exception while starting resolution activity", e); } } /** * shows toast message. */ public void showmessage(string message) { toast.maketext(this, message, toast.length_long).show(); } /** * getter {@code googleapiclient}. */ public googleapiclient getgoogleapiclient() { return mgoogleapiclient; } }
error
04-21 11:36:52.295: e/androidruntime(6468): fatal exception: asynctask #3 04-21 11:36:52.295: e/androidruntime(6468): process: com.example.infoscrapper2, pid: 6468 04-21 11:36:52.295: e/androidruntime(6468): java.lang.runtimeexception: error occured while executing doinbackground() 04-21 11:36:52.295: e/androidruntime(6468): @ android.os.asynctask$3.done(asynctask.java:300) 04-21 11:36:52.295: e/androidruntime(6468): @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:355) 04-21 11:36:52.295: e/androidruntime(6468): @ java.util.concurrent.futuretask.setexception(futuretask.java:222) 04-21 11:36:52.295: e/androidruntime(6468): @ java.util.concurrent.futuretask.run(futuretask.java:242) 04-21 11:36:52.295: e/androidruntime(6468): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) 04-21 11:36:52.295: e/androidruntime(6468): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) 04-21 11:36:52.295: e/androidruntime(6468): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) 04-21 11:36:52.295: e/androidruntime(6468): @ java.lang.thread.run(thread.java:864) 04-21 11:36:52.295: e/androidruntime(6468): caused by: java.lang.illegalargumentexception: id must provided. 04-21 11:36:52.295: e/androidruntime(6468): @ com.google.android.gms.drive.internal.l.getfile(unknown source) 04-21 11:36:52.295: e/androidruntime(6468): @ com.example.infoscrapper2.gdriveloadtext$retrievedrivefilecontentsasynctask.doinbackgroundconnected(gdriveloadtext.java:84) 04-21 11:36:52.295: e/androidruntime(6468): @ com.example.infoscrapper2.gdriveloadtext$retrievedrivefilecontentsasynctask.doinbackgroundconnected(gdriveloadtext.java:1) 04-21 11:36:52.295: e/androidruntime(6468): @ com.example.infoscrapper2.apiclientasynctask.doinbackground(apiclientasynctask.java:63) 04-21 11:36:52.295: e/androidruntime(6468): @ android.os.asynctask$2.call(asynctask.java:288) 04-21 11:36:52.295: e/androidruntime(6468): @ java.util.concurrent.futuretask.run(futuretask.java:237) 04-21 11:36:52.295: e/androidruntime(6468): ... 4 more
Comments
Post a Comment