[英]What is the most efficient way to download Google Drive images in Android?
I'm writing an app which requires that images be downloaded from a Google Drive. 我正在编写一个要求从Google云端硬盘下载图片的应用。 I am currently doing this using the following code:
我目前正在使用以下代码执行此操作:
protected void downloadFromDrive(Context context) {
InputStream input = null;
FileOutputStream output = null;
try {
HttpRequest request = GoogleDriveWorker.get(context)
.getDrive()
.getRequestFactory()
.buildGetRequest(new GenericUrl(getUri()));
input = request.execute().getContent();
output = context.openFileOutput(getImageFilename(), Context.MODE_PRIVATE);
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = input.read(buffer)) != -1) {
output.write(buffer, 0, len);
}
} catch (UnrecoverableKeyException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(output!=null)
output.close();
if(input!=null)
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getUri() {
return mUri;
}
GoogleDriveWorker
is just a class that gets a google drive with the credentials we're using. GoogleDriveWorker
只是一个使用我们使用的凭据获取Google驱动器的类。 Anyway, most of the examples I can find use this basic structure to download a file from an InputStream
and put it to an OutputStream
, but the download rate is rather slow. 无论如何,我可以找到的大多数示例都使用此基本结构从
InputStream
下载文件并将其放入OutputStream
,但是下载速度相当慢。
Firstly, can I speed it up by using a more sophisticated method than synchronously buffering the InputStream
to the OutputStream
a kilobyte at a time? 首先,我可以加速这一过程通过使用比同步缓存更复杂的方法
InputStream
到OutputStream
一次一个千字节? It strikes me that I should try to read the InputStream
on a different thread, and output to the OutputStream
as kilobyte chunks become available using a queue of chunks. 令我惊讶的是,我应该尝试在不同的线程上读取
InputStream
,并在使用千字节块的情况下千字节块变得可用时输出到OutputStream
。 Tying the read code and the write code together seems clunky, and they will surely slow each other down. 将读取代码和写入代码捆绑在一起似乎很麻烦,而且它们肯定会彼此放慢速度。
Secondly, would changing the buffer size affect the data rate at all? 其次,更改缓冲区大小是否会完全影响数据速率? A kilobyte seems small, but on a mobile connection maybe it's not that small.
千字节看起来很小,但在移动连接上可能并不小。 Then again the larger the chunk, the larger the wait from each section of the read/write loop.
然后,块越大,来自读/写循环的每个部分的等待时间就越大。 Is using a different-sized buffer worth considering?
是否值得考虑使用其他大小的缓冲区?
I don't think there's a more sophisticated method than what you did. 我认为没有比您做的更复杂的方法了。 You could probably make some experiments with larger chunks (for example few hundred KB) and measure the tiem.
您可能会用更大的块(例如几百KB)进行一些实验,并测量卷边。 I think it was faster.
我认为速度更快。
Also check the drive/java-api-client-library documentation about the chunk size. 还要查看关于块大小的drive / java-api-client-library文档。 I think there was some explanation about it, but I'm not 100% sure about that.
我认为对此有一些解释,但我不确定100%。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.