java - Android: How to get the uploaded file URL in Google Drive -
i can upload files google drive account need url (link) of file use later? example: https://docs.google.com/file/d/ob-xxxxxxxxxxx/edit
this method upload file google drive:
private void savefiletodrive() { thread t = new thread(new runnable() { @override public void run() { try { // create uri real path string path; path = "/sdcard/dcim/camera/a.png"; mfileuri = uri.fromfile(new java.io.file(path)); contentresolver cr = uploadactivity.this.getcontentresolver(); // file's binary content java.io.file filecontent = new java.io.file(mfileuri.getpath()); filecontent mediacontent = new filecontent(cr.gettype(mfileuri), filecontent); showtoast("selected " + mfileuri.getpath() + "to upload"); // file's meta data. file body = new file(); body.settitle(filecontent.getname()); body.setmimetype(cr.gettype(mfileuri)); com.google.api.services.drive.drive.files f1 = mservice.files(); com.google.api.services.drive.drive.files.insert i1 = f1.insert(body, mediacontent); file file = i1.execute(); if (file != null) { showtoast("uploaded: " + file.gettitle()); } } catch (userrecoverableauthioexception e) { startactivityforresult(e.getintent(), request_authorization); } catch (ioexception e) { e.printstacktrace(); showtoast("transfer error: " + e.tostring()); } } }); t.start(); }
i got it, need file id , adding string in following:
private void savefiletodrive() { thread t = new thread(new runnable() { @override public void run() { try { string imagelink = null; // create uri real path string path; path = "/sdcard/dcim/camera/b.mov"; mfileuri = uri.fromfile(new java.io.file(path)); contentresolver cr = uploadactivity.this.getcontentresolver(); // file's binary content java.io.file filecontent = new java.io.file(mfileuri.getpath()); filecontent mediacontent = new filecontent(cr.gettype(mfileuri), filecontent); showtoast("selected " + mfileuri.getpath() + " upload"); // file's meta data. file body = new file(); body.settitle(filecontent.getname()); body.setmimetype(cr.gettype(mfileuri)); com.google.api.services.drive.drive.files f1 = mservice.files(); com.google.api.services.drive.drive.files.insert i1 = f1.insert(body, mediacontent); file file = i1.execute(); if (file != null) { showtoast("uploaded: " + file.gettitle()); } if (file.getdownloadurl() != null && file.getdownloadurl().length() > 0) { imagelink = "https://drive.google.com/open?id=" + file.getid() +"&authuser=0"; system.out.println("imagelink: " + imagelink); } } catch (userrecoverableauthioexception e) { startactivityforresult(e.getintent(), request_authorization); } catch (ioexception e) { e.printstacktrace(); showtoast("transfer error: " + e.tostring()); } } }); t.start(); }
Comments
Post a Comment