Is it possible to have random access writes to a Google Drive file from the Android API?

笑着哭i 提交于 2020-01-16 01:53:09

问题


I'm trying to record audio and create a wav file in Google Drive with the Android API. I have a first pass where I write the header with a file length of 0 bytes because I don't know how long the recorded audio will be and I don't want to keep it all in memory. Once the record is finished I seek back to byte 4 and write the length of the file.

This works great when using a RandomAccessFile but I can't figure out how to do something using the Google Drive API. https://developers.google.com/drive/android/files

The DriveContents class (https://developers.google.com/android/reference/com/google/android/gms/drive/DriveContents) doesn't hint at any way to seek within the contents. Any ideas?


回答1:


I don't think it will be possible this way. Google drive Apis (V2, V3, GDAA) all accept file streams (i.e. serial streams) as contents. You are probably aware that you construct metadata + contents and upload (create, patch, update).

Since you need to modify your file's header after you're finished writing to the file, the only way to do it is to finish everything in a local file and then hand it's stream to the GooDrive Api. The update/patch will not help you in this case since your header is a part of a chunk of bytes known to GooDrive Apis as contents.

It is logical, V2 and V3 are straight REST apis, so they send the bytes up in a serial fashion. There is some hope GDAA may do it one day (see 'Lifecycle of a Drive file'), but I wouldn't hold my breath.

Good Luck




回答2:


Try using FileChannel for random access.

You can get the FileChannel with FileOutputStream#getChannel, then seek using FileChannel#position(long). Writing to the OutputStream will then start from the set position.

Note: I would advise to flush the OutputStream before accessing the FileChannel.



来源:https://stackoverflow.com/questions/34514640/is-it-possible-to-have-random-access-writes-to-a-google-drive-file-from-the-andr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!