简体   繁体   中英

Estimating the time to read a large file from SD Card on Android

I want to get an idea (ballpark) of how much time it takes to read a large file (50MB to 100MB) stored on Android's SD card. I'm using the following code on Android 2.3.3 on a Google Nexus One. Will this give me a reasonable estimate ? Is there a standard method that I should be using ? Also, will using native code improve my file I/O performance ?

Thanks.

public void readFileFromSDCard() throws IOException
{
    long start = System.currentTimeMillis();
    long size = 0;
    File file = new File(OVERLAY_PATH_BASE + "base-ubuntu.vdi");
    try {

        InputStream in = new FileInputStream(file);
        try {
            byte[] tmp = new byte[4096];
            int l;
            while ((l = in.read(tmp)) != -1) {
                size = size + 4096;
            }
        } finally {
            in.close();
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } 
    long end = System.currentTimeMillis();
    Log.e(LOG_TAG,
            "Reading file " + file.getAbsolutePath() + " with " + size  + "bytes took " + (end-start) + " ms.");

}

The easiest thing to do for this code would be to time how long each while loop iteration takes and use that to estimate how long is left. For example, if you process 0.1Mb in one iteration in one second and you have 1Mb in total to do, you can guess that you have about 9 seconds to go.

SD card performance varies a lot between different phones and the speed can be unpredictable even on the same phone (eg I get large random delays on my phone for small files). I don't think it's worth doing anything more sophisticated than the above myself, such as benchmarking the phone before processing the file. A percent bar is probably enough for most uses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM