Google Play Services DriveApi change file parent - android

I can't seem to find out how to move file into another folder using Drive.DriveApi from Google Play Services.
Only thing I see is using driveFolder.createFile(GoogleApiClient, MetadataChangeSet, Contents) but that's only if I'm going to upload a new file.
MetadataChangeSet.Builder() doesn't have any methods that point to file parent.
Is that impossible?

Changing parents is not currently exposed in the Google Play Services API. You can fall back to the web RESTful API to do this if you need to: parents collection. Just get the resourceId from the DriveId.

Related

Is there any way to get Google Sheets fileid from file picker intent?

In my Android app I would like the user to open a Google Sheets document to import data from it into the app database.
It's easy to select a file with ACTION_GET_CONTENT / ACTION_OPEN_DOCUMENT and with MIME filter application/vnd.google-apps.spreadsheet. (It will be a virtual file.)
It's also easy to get the content of a Google Sheets document with the Google Sheets API based on the Drive filed id (with user-granted spreadsheets.readonly scope):
Sheets.Builder(transport, jsonFactory, appCredential)
.build()
.spreadsheets()
.get(fileid)
.execute()
But the first step returns the Uri of the file on the device, and the second step requires the file id of the Drive file.
How can I determine the second from the first?
The only poor solution I found was to use the Google Drive API: I search the Sheets file with its name I got from the intent, and I get the id of it... It requires the drive.metadata.readonly scope, which is a restricted scope, and doh... it's not a viable solution anyway...
I can download a pdf from the uri of the virtual file, but it's not suitable for importing data.
If anyone knows the answer, a thousand thanks for it!
It depends on what the type of fileId is. (I am not familiar with that particular API).
In general, you need to use ContentResolver to convert the Uri into some usable type such as InputStream, FileDescriptor, etc.
There is also the DocumentFile utility class to work with Document URIs.

Get file from Google Drive via shared link

I'm making something like online journal app, that will download "journal file" from Google Drive (via shared link) once and will update that file if it changes. Please, can anyone point me to some guides how to do it. I already tried to pin file from drive, but I don't really understand what to do next..
Downloading Files from Google Drive:
To download a Google Drive file via link, try this (from this tutorial):
https://drive.google.com/uc?export=download&id=FILE_ID
Just replace the FILE_ID with the original fileID found in the Drive URL.
Additonal notes:
You can download files using the DRIVE REST API
To download files, you make an authorized HTTP GET request to the file's resource URL and include the query parameter alt=media. For example:
GET https://www.googleapis.com/drive/v3/files/0B9jNhSvVjoIVM3dKcGRKRmVIOVU?alt=media
Authorization: Bearer ya29.AHESVbXTUv5mHMo3RYfmS1YJonjzzdTOFZwvyOAUVhrs
Downloading the file requires the user to have at least read access. Additionally, your app must be authorized with a scope that allows reading of file content. For example, an app using the drive.readonly.metadata scope would not be authorized to download the file contents. Users with edit permission may restrict downloading by read-only users by setting the viewersCanCopyContent field to true.
Updating files in Google Drive
Make an HTTP Request to Google Drive using PATCH. The PATCH method requests that a set of changes described in the request entity be applied to the resource identified by the Request- URI. Things to take note of are:
Parameters
Authorization
Request Body

Cannot access Google Drive file

I am making a notepad app using Google Drive. Whenever user creates a file in the app, it also creates a file in the Google Drive. And user can enter the text and save the file, the unsaved text will get committed whenever the internet is available. I am managing the update and create processes within the app using the DriveId.
If the user wants to use the files with the alternative device using my app, for that I also have the option called DriveId import. By clicking the option DriveId import user will be prompted with the input box for entering the existing DriveId. Using the DriveId I thought of opening the files, But it was giving an error.
Then I saw an answer given in this SO which clearly says DriveId can be used only inside the app and device which created the file.
I also found a similar question like mine in here SO But I can’t get my problem solved. I have taken ResourceId using result.getDriveFolder().getDriveId().getResourceId()
How to read the data’s programmatically using the ResourceID? As said in the above answer here I don’t want to change the track and go into Drive REST API. Is there a way that I can read the data using Google Drive Android API ? I have done all the development process, but in the ending when I try to access from other device it is giving the error. Totally struck.
If I can only read the data using REST API any simple code will be appreciated. Thanks in advance.
Finally Solved the DriveId Issue without REST API.
To get DriveId on the alternative device. You will need resourceId. You can use following code:-
String resourseId = "xxxxxxxxxxxxx"
Drive.DriveApi.fetchDriveId(mGoogleApiClient,resourseId).setResultCallback(idCallBack);
private ResultCallBack<DriveApi.DriveResult> idCallBack = new ResultCallback<DriveApi.DriveIdResult>() {
#Override
public void onResult(DriveApi.DriveIdResult driveIdResult) {
msg.Log("onResult");
DriveId id = driveIdResult.getDriveId(); //Here you go :)
}
}

Android upload/download text files from sdcard to Google Drive with Google Play Services

I want to implement Google Drive upload/download txt file to my Android app using Google Play Services, but I don't find a right way to do it.
In dropbox is very easy:
File file = new File("working-draft.txt");
FileInputStream inputStream = new FileInputStream(file);
Entry response = mDBApi.putFile("/magnum-opus.txt", inputStream,
file.length(), null, null);
Log.i("DbExampleLog", "The uploaded file's rev is: " + response.rev);
But for Google Drive with Google Play Services I have not found a simple way like that. Do you know how to?
By the way, I would like to use my own credentials (google service account) instead an user account, what is the way to implement it with Google Play Services?
Thanks in advance
Take a look here, it does everything you've mentioned. I don't know about the 'simple' requirement, though. Or go through the official DEMO code here. But it is definitely not a 'two-liner' endeavor.

Unable to get "thumbnailLink" from Google Drive Rest API V3

I am trying to get all images in a google drive folder. I am able to do so but still not getting the thumbnailLink.
FileList result = mService.files().list()
.setPageSize(10)
.setQ("'"+parentFile.getId()+"'" + " in parents and mimeType='image/jpeg' ")
.setFields("nextPageToken, files(contentHints,description,fileExtension,folderColorRgb,fullFileExtension,
iconLink,id,kind,md5Checksum,mimeType,name,originalFilename,
thumbnailLink,videoMediaMetadata,webContentLink,webViewLink)")
.execute();
Here's the Url that is being hit from the code
https://www.googleapis.com/drive/v3/files?fields=nextPageToken,%20files(fileExtension,
folderColorRgb,iconLink,id,thumbnailLink,kind,mimeType,name,
originalFilename,webContentLink,webViewLink)
&pageSize=20&q=0B-u5D758kMAgfmh3SnNabDJRaENISXVUR2kwWEw3TWtQSGJpbWhUVTJLaUZyRWtmM3lGTjA
%20in%20parents%20and%20mimeType%3Dimage/jpeg%20
The Same data parameters (above) yields the thumbnail when use the API Explorer
https://developers.google.com/apis-explorer/#search/drive.files.list/m/drive/v3/drive.files.list
The Google Drive APIs reference on SCOPES refers to excluding thumbnails a few times but doesn't help much defining which scope allows thumbnails to be returned.
I upped my scope from
https://www.googleapis.com/auth/drive.metadata.readonly to
https://www.googleapis.com/auth/drive and my Node app started retrieving thumbnails for the first time.
This iOS specific post describing the same issue suggests using https://www.googleapis.com/auth/drive.photos.readonly
On the basis that we should use the least permissive scope possible I ended up adding the drive.photos.readonly scope to my original scope giving me
var SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly', 'https://www.googleapis.com/auth/drive.photos.readonly']
Of course, I'm OT for your platform but the code will be similar I'm sure.

Categories

Resources