繁体   English   中英

有没有快速的方法将文件复制到SD卡

[英]Is there a fast way to copy files to sdcard

我有一个功能将存储在资产中的jpeg复制到SD卡。 它有效,但非常慢。 averg文件大小约为600k。 有没有更好的方法来做到这一点,代码:

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;
}

一次读写多个字符。 16KB可能是一个合理的缓冲区大小,但可以随意进行实验。

您应该尝试使用Buffer Classes BufferedInputStreamBufferedOutputStream读写

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();

祝好运

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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