简体   繁体   English

在Android中下载Google云端硬盘图像的最有效方法是什么?

[英]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? 首先,我可以加速这一过程通过使用比同步缓存更复杂的方法InputStreamOutputStream一次一个千字节? 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.

相关问题 Android最有效的保存和加载图像的方法是什么? - Android what's the most efficient way to save and load images? 在Android中显示表格的最有效方法是什么 - What is the most efficient way to display a table in Android 使用Android相机的最有效方法是什么? - What is the most efficient way to work with the Android camera? 最有效的显示图像的方式 - most efficient way of displaying images 从URL Android加载大量图像的最有效方法 - Most efficient way to load a lot of images from URL Android Android App:处理大量图像的水平滚动的最有效方法 - Android App: Most Efficient Way to Handle Horizontal Scrolling With Lots of Images 在Android用户界面中旋转和显示图像的最有效方法是什么? - What is the most efficient way to rotate and display an image in an Android UI? 在android中,比较两个文件以确定它们是否相同的最有效方法是什么? - In android, what is the most efficient way to compare two files to determine if they are identical? 什么是最有效的(电池)重复动作在android中的方法? - what is the most efficient(battery) way to repeat an action in android? 在Android的SharedPreferences中存储数组的最有效方法是什么? - What is the most efficient way to store an array in SharedPreferences in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM