Android Question upload file over 30MB to Google Drive

Ibbiu

Member
Licensed User
Longtime User
I followed this thread
https://www.b4x.com/android/forum/threads/googledrive-via-rest-api-v3-small-testproject.95778/

I need to upload a zip file of over 30MB to google drive, when i start i get an out of memory error
I have seen that files over 27MB have this problem (lower all ok ). Is there a possibility to increase the buffer capacity?

This is the part of googleDrive module that generates the error

B4X:
Dim data() As Byte
Dim RR As String = "--" & boundary & EOL & "Content-Type: application/json; charset=UTF-8" & EOL & EOL & Jg.ToString & EOL & EOL & "--" & boundary & EOL & "Content-Type: " & ContentType & EOL & EOL
data = (RR).GetBytes("UTF-8")
out2.WriteBytes( data, 0, data.Length)                                                'Write it To the Stream. 
File.Copy2(In, out2)                                                                              'Add the File itself To the Stream.            [B]  error line[/B]
data = (EOL & EOL & "--" & boundary & "--").GetBytes("UTF-8")    'Add the final boundary to the POST
out2.WriteBytes(data, 0, data.Length)                                                 'Write it To the Stream Then make the Bytes the complete Stream.
data = out2.ToBytesArray

This is log.

java.lang.OutOfMemoryError
at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:91)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:201)
at anywheresoftware.b4a.objects.streams.File.Copy2(File.java:362)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:4442)
at android.view.View$PerformClick.run(View.java:18473)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
at dalvik.system.NativeStart.main(Native Method)

Thanks!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You need to switch to the "resumable upload" protocol: https://developers.google.com/drive/api/v3/manage-uploads

From a quick look it doesn't look too complicated to implement. This will allow you to avoid loading the complete file into memory.
You will need to use OkHttpUtils2 source code and change line 160 from:
B4X:
req.InitializePost(Link, In, length)
To:
B4X:
req.InitializePut(Link, In, length)
Or better create a new PutFile sub.
 
Upvote 0

Ibbiu

Member
Licensed User
Longtime User
I tried, but my knowledge is not so advanced. I solved it by cutting the file into smaller files.
 
Upvote 0
Top