Reputation: 4220
I can download the file but I can't copy it's contents.
I'm using this code:
HttpResponse resp = gDrive.getRequestFactory().buildGetRequest(new GenericUrl(driveFile.getDownloadUrl())).execute();
InputStream in= resp.getContent();
FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
while(in.available()>0) {
fos.write(in.read());
}
But the in.available() returns always 0. And yes, the content is there. If I just use in.read(), the first character comes ok.
The files are really small, just a few bytes or even empty.
Thanks.
Upvotes: 0
Views: 124
Reputation: 4033
Do not use available()
. It is explicitly noted in the documentation that available()
will only tell you how many bytes definitely are available, but that it may return 0 if it feels like it.
You want to use read(byte[])
to read chunks of undefined size into a buffer until it returns -1, and the write the corresponding amount of bytes out using write(byte[], int, int)
- something like this:
byte[] buf = new byte[1024];
while (true) {
int bytes = in.read(buf);
if (bytes < 0) break;
fos.write(buf, 0, bytes);
}
The Guava library provides a function for that.
Upvotes: 1