2012-09-17 38 views
0

我有一个功能可以将存储在资产中的JPEG复制到SD卡上。它工作,但非常非常缓慢。 averg文件大小约为600k。有没有更好的方式来做到这一点, 代码:有没有一种快速的方法将文件复制到SD卡

void SaveImage(String from, String to) throws IOException { 
    // opne file from asset 
    AssetManager assetManager = getAssets(); 
    InputStream inputStream; 
    try { 
    inputStream = assetManager.open(from); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    return; 
    } 

    // Open file in sd card 
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); 
    OutputStream outStream = null; 
    File file = new File(extStorageDirectory, to); 
    try { 
    outStream = new FileOutputStream(file); 
    } catch (FileNotFoundException e) { 
    e.printStackTrace(); 
    return; 
    } 

    int c; 
    while ((c = inputStream.read()) != -1) { 
    outStream.write(c); 
    } 

    outStream.close(); 
    inputStream.close(); 
    return; 
} 
+0

可能重复:http://stackoverflow.com/questions/4447477/android-how-to -copy-files-in-assets-to-sdcard –

回答

0

你应该使用BufferBufferedInputStreamBufferedOutputStream

InputStream inputStream; 
BufferedInputStream bis; 
try { 
    inputStream = assetManager.open(from); 
    bis = new BufferedInputStream(inputStream); 
} catch (IOException e) { 
... 
... 
try { 
    outStream = new BufferedOutputStream(new FileOutputStream(file)); 
} catch (FileNotFoundException e) { 
... 
... 
    while ((c = bis.read()) != -1) { 
    ... 
    } 
... 
... 

bis.close(); 

好运尝试阅读和写作

+0

嗨,哇,什么是速度差异,我认为它会更复杂,然后加速它:) –

2

读写同时多个字符。尽管可以随意尝试,但16KB可能是一个合理的缓冲区大小。

+0

嗨,听起来像一个很好的想法,我该怎么做? –

+0

@Tedpottel:看到接受的答案在http://stackoverflow.com/questions/4447477/android-how-to-copy-files-in-assets-to-sdcard – CommonsWare

相关问题