Hello,
I will show you my new library which make it easy to connect and work with Google Drive.
Until this moment the library can :
All what you need is your access key and the secret key from the google developer console :
https://console.developers.google.com/
Methods :
Initialize(CallbackModul, Eventname, ClientID, ClientSecret)
ConnectToDrive
Here is the start point. If the connection is successfully, the event Drive_Connected raised.
If no Network connection available the event Drive_NoInternetConnection raised.
UploadFile(Title, UploadDir, UploadFilename, FileID, FolderID) as String
With this function you can upload a file on drive. The param "title" is the name of the file in Google Drive. If you want the name is the same as the filename, then pass it with "".
UploadDir and UploadFilename is the path of the file on the local device.
If you upload the file the first time, you can pass the FileID with "". Else, if you want to update a file, you need this ID. Without this ID google created every time a new file (test.jpg, test(1).jpg, test(2)....)
The event "FileUploaded(FileID as string)" will be raised with the FileID, if the upload was successfully.
DownloadFile(FileID, LocalPath, LocalFilename)
Save the file on the device on the passed path. The FileID is needed to find this file on drive. If you don't know the FileID, you can try to find it with the "SearchForFile" function.
SearchForFile(Filename)
If you don't know the FileID of a file, you can try to find it with this function.
The Event "FileFound(ID as String)" will be raised in any case. If the file was found, the ID will return. If not, the ID return an empty string "".
SearchForFolderID(Foldername)
The same like SearchForFile, but only for folders.
ShowTheFileList
Returns a list with all filenames on Drive.
GetMetadata(FileID)
Some information can download about a file. The result returns as a map with the follow entrys :
"kind", "fileid", "selflink", "iconLink", "title", "mimeType", "createdDate", "modifiedDate", "downloadUrl", "fileExtension"
ResetAll
Delete internal oauth files to restart the connection. Should call at start of application or with a follow "ConnectToDrive"-method.
Monitoring :
This function mirrors a file between the device and Google Drive. The path of this file is "Apps/vad_YourFoldername". In this folder the mirrored file will upload.
Monitoring_Start(Foldername, LocalDir, LocalFile, Monitoring_Interval, DoNotTransmitUpdates)
- Foldername is the name of the folder on Google Drive. The file will saved in the folder "Apps/vad_YourFolderName". But you don't need this name for any access.
- LocalDir und LocalFile is the path for the file on your device.
- Monitoring_Interval is the duration between check for actuality of the file. Time in Minutes
- DoNotTransmitUpdates prevent the function to update or download the file. Only the Events "DriveFileIsNewer" or "DriveFileIsOlder" will be raised. If you want to make an update catch the event and call "Monitoring_ForceTransmission".
To reset the folderID or fileID on Google Drive, call ResetMonitoring. Else ever the same fileID and FolderID will uses until you deinstall the application.
You can manually File.Delete(File.DirInternal, "fileid.txt"), too. It's the same effect. Recall the Monitoring_Start after this command.
Monitoring_Start2(Foldername, LocalDir, LocalFile, Monitoring_Interval, DoNotTransmitUpdates, ForceFileID)
The same like Monitoring_Start, but with ForceFileID. With this method will not create new file on first init, but the pass FileID will used. (No controlling for correctness of the ID) Good for switching between further files. To keep the old fileID, use Monitoring_GetID and save this content.
Monitoring_Refresh
Start the check manual.
Monitoring_Stop
Stops the check interval.
Monitoring_ForceTransmission
Transfer the monitoring file if needed. This function ignores the command "TransferFile" in "Monitor_Start" function.
ResetMonitoring
Resets the access data for monitoring. Helpful to create new monitoring file, changing folder or for tests.
Monitoring_GetID as Map
Returns a map with the storaged IDs from monitoring service (contents "fileid" and "folderid")
If config file is not create, a uninitialized map will return
Propertys :
Monitoring_OnlyWlan as Boolean (False)
If True, the Monitoring function will not work without WLAN.
Events :
Tip :
Maybe this library helps someone.
Update 29.09 :
Fixed failures with Monitor_Stop
Update 29.09 V2 :
Fixed a problem with fileupload
Adds method "ResetMonitoring" for reset the monitoring settings
Update 29.09 V3 :
Added a better method to proof whether the file on gDrive is newer.
Prevent a failure message.
Last Update :
Fixed some failures. Upload the source code.
I will show you my new library which make it easy to connect and work with Google Drive.
Bis jetzt beherrscht diese lib folgende Punkt :
- Es verbindet sich automatisch mit Google Drive und behält diese Verbindung auch beim nächsten Start der App. Die Zugangsdaten inkl. generierter Token werden in einer Textdatei unter File.DirIntern gespeichert.
- Hochladen einer Datei
- Download einer Datei (FileID wird benötigt)
- Suchen der FileID durch den Dateinamen
- Metadaten der Datei auslesen
- Monitoring einer Datei. Hierbei wird automatisch eine Datei mit Google Drive abgeglichen. Sollte die Drive Datei neuer sein, wird die lokale Datei, wenn gewünscht, überschrieben. Genauso umgekehrt. Beispiel dafür wäre eine Backupfunktion oder auch das nutzen einer Konfigurationsdatei/Datenbank zwischen zwei verschiedenen Geräten.
Until this moment the library can :
- Connect and reconnect automatically. The token will stored in a file on internal storage. The refresh of the access token the lib makes alone if needed.
- Upload a file
- Download a file (FileID is needed)
- Find the FileID with filename. This function is not yet fully matured, because it's only possible to search for the filename. If two files with the same name on the drive storage, the first founded will be choosed.
- Metatag of files (FileID is needed)
- Monitoring of a file. This procedure automatically mirror a local file with a file on Google Drive. To example, the setting file on Google Drive was changed by an another app (i.e. from tablet version). This file will downloaded to the device with older version of this file. And upload in case of newer file, too.
All what you need is your access key and the secret key from the google developer console :
https://console.developers.google.com/
Methods :
Initialize(CallbackModul, Eventname, ClientID, ClientSecret)
ConnectToDrive
Here is the start point. If the connection is successfully, the event Drive_Connected raised.
If no Network connection available the event Drive_NoInternetConnection raised.
UploadFile(Title, UploadDir, UploadFilename, FileID, FolderID) as String
With this function you can upload a file on drive. The param "title" is the name of the file in Google Drive. If you want the name is the same as the filename, then pass it with "".
UploadDir and UploadFilename is the path of the file on the local device.
If you upload the file the first time, you can pass the FileID with "". Else, if you want to update a file, you need this ID. Without this ID google created every time a new file (test.jpg, test(1).jpg, test(2)....)
The event "FileUploaded(FileID as string)" will be raised with the FileID, if the upload was successfully.
DownloadFile(FileID, LocalPath, LocalFilename)
Save the file on the device on the passed path. The FileID is needed to find this file on drive. If you don't know the FileID, you can try to find it with the "SearchForFile" function.
SearchForFile(Filename)
If you don't know the FileID of a file, you can try to find it with this function.
The Event "FileFound(ID as String)" will be raised in any case. If the file was found, the ID will return. If not, the ID return an empty string "".
SearchForFolderID(Foldername)
The same like SearchForFile, but only for folders.
ShowTheFileList
Returns a list with all filenames on Drive.
GetMetadata(FileID)
Some information can download about a file. The result returns as a map with the follow entrys :
"kind", "fileid", "selflink", "iconLink", "title", "mimeType", "createdDate", "modifiedDate", "downloadUrl", "fileExtension"
ResetAll
Delete internal oauth files to restart the connection. Should call at start of application or with a follow "ConnectToDrive"-method.
Monitoring :
This function mirrors a file between the device and Google Drive. The path of this file is "Apps/vad_YourFoldername". In this folder the mirrored file will upload.
Monitoring_Start(Foldername, LocalDir, LocalFile, Monitoring_Interval, DoNotTransmitUpdates)
- Foldername is the name of the folder on Google Drive. The file will saved in the folder "Apps/vad_YourFolderName". But you don't need this name for any access.
- LocalDir und LocalFile is the path for the file on your device.
- Monitoring_Interval is the duration between check for actuality of the file. Time in Minutes
- DoNotTransmitUpdates prevent the function to update or download the file. Only the Events "DriveFileIsNewer" or "DriveFileIsOlder" will be raised. If you want to make an update catch the event and call "Monitoring_ForceTransmission".
To reset the folderID or fileID on Google Drive, call ResetMonitoring. Else ever the same fileID and FolderID will uses until you deinstall the application.
You can manually File.Delete(File.DirInternal, "fileid.txt"), too. It's the same effect. Recall the Monitoring_Start after this command.
Monitoring_Start2(Foldername, LocalDir, LocalFile, Monitoring_Interval, DoNotTransmitUpdates, ForceFileID)
The same like Monitoring_Start, but with ForceFileID. With this method will not create new file on first init, but the pass FileID will used. (No controlling for correctness of the ID) Good for switching between further files. To keep the old fileID, use Monitoring_GetID and save this content.
Monitoring_Refresh
Start the check manual.
Monitoring_Stop
Stops the check interval.
Monitoring_ForceTransmission
Transfer the monitoring file if needed. This function ignores the command "TransferFile" in "Monitor_Start" function.
ResetMonitoring
Resets the access data for monitoring. Helpful to create new monitoring file, changing folder or for tests.
Monitoring_GetID as Map
Returns a map with the storaged IDs from monitoring service (contents "fileid" and "folderid")
If config file is not create, a uninitialized map will return
Propertys :
Monitoring_OnlyWlan as Boolean (False)
If True, the Monitoring function will not work without WLAN.
Events :
Connected(Settings as Map)
If the connection was successfully.
FileListResult(Filelist as List)
Returns the list of names on Google Drive
FileDownloaded
If download from a file was successfully.
FileFound(ID as String)
Returns the result from SearchForFileID. If anything found, the FileID returns. Else "".
FolderFound(ID as String)
Returns the result from SearchForFolderID. If anything found, the FolderID returns. Else "".
FileUploaded(FileID as string)
If a file was correctly uploaded.
UserAbortsRequest
User denied the access.
GetMetadata(Data as Map)
Returns some metadata from a file :
kind,fileid,selflink,iconlink,title,mimeType,createDate,modifiedDate,downloadUrl,fileExension,trashed
NoInternetConnection
Raise if on connection process no network connection available.
DriveFileIsNewer
DriveFileIsOlder
If the monitoring file on drive older or newer, on of this event raises.
If the connection was successfully.
FileListResult(Filelist as List)
Returns the list of names on Google Drive
FileDownloaded
If download from a file was successfully.
FileFound(ID as String)
Returns the result from SearchForFileID. If anything found, the FileID returns. Else "".
FolderFound(ID as String)
Returns the result from SearchForFolderID. If anything found, the FolderID returns. Else "".
FileUploaded(FileID as string)
If a file was correctly uploaded.
UserAbortsRequest
User denied the access.
GetMetadata(Data as Map)
Returns some metadata from a file :
kind,fileid,selflink,iconlink,title,mimeType,createDate,modifiedDate,downloadUrl,fileExension,trashed
NoInternetConnection
Raise if on connection process no network connection available.
DriveFileIsNewer
DriveFileIsOlder
If the monitoring file on drive older or newer, on of this event raises.
Tip :
If you use the monitoring function and the application don't change anything on the file in a background process, use this :
This will conserve your battery.
B4X:
Sub Activity_Resume
Drive.Monitoring_Start("yourEvent", File.DirInternal, YOUR_FILE, 1, FALSE)
End Sub
Sub Activity_Pause (UserClosed As Boolean)
clsDrive.Monitoring_Stop
End Sub
Maybe this library helps someone.
Update 29.09 :
Fixed failures with Monitor_Stop
Update 29.09 V2 :
Fixed a problem with fileupload
Adds method "ResetMonitoring" for reset the monitoring settings
Update 29.09 V3 :
Added a better method to proof whether the file on gDrive is newer.
Prevent a failure message.
Last Update :
Fixed some failures. Upload the source code.
Attachments
Last edited: