简体   繁体   English

如何将文本/纯文本文件从Google云端硬盘复制到Android App本地文件

[英]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.

相关问题 将Google云端硬盘实施到Android应用并从Google云端硬盘下载文件 - Google Drive Implementation to Android app and Downloading the file from Google drive 将文件从Android应用上传到Google驱动器 - Upload file from android app to google drive 如何从Android应用程序共享Google Drive文件? - How to share Google Drive file from Android app? 如何从Android应用下载存储在Google云端硬盘中的文件? - How to download a file stored on Google Drive from an Android app? 如何将视频格式文件从 Android 应用推送到 Google Drive? - How to push a video format file to the Google Drive from an Android app? 从谷歌驱动器中读取android中的App文件夹中的文件 - Read file from App folder in android from google drive 如何以编程方式从 Android 中的 Google 驱动器选择文件下载并保存在本地目录中? - How to download and save in local directory from Google drive selected file in Android programmatically? 从Android应用上传Google Drive Sqlite数据库文件 - Google Drive Sqlite db file upload from android app 通过文件扩展名在我的 Android 应用程序中打开来自 Google Drive 的文件 - Opening files from Google Drive in my Android app by file extension 在Android应用中从Google Drive更新.json文件 - Update .json file from google drive in android app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM