[英]How to copy a text/plain file from Google Drive to Android App local File
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. 但是in.available()始终返回0。是的,内容在那里。 If I just use in.read(), the first character comes ok.
如果我只使用in.read(),第一个字符就可以了。
The files are really small, just a few bytes or even empty. 这些文件很小,只有几个字节,甚至是空的。
Thanks. 谢谢。
Do not use available()
. 不要使用
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. 在文档中明确指出,
available()
只会告诉您肯定有多少字节可用,但是如果感觉到它可能会返回0。
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: 您想使用
read(byte[])
将未定义大小的块读入缓冲区,直到返回-1,然后使用write(byte[], int, int)
写入相应数量的字节-像这样:
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 . Guava库为此提供了一个功能 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.