0

アセットに保存されているjpegをsdカードにコピーする機能があります。動作しますが、非常に遅いです。平均ファイル サイズは約 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;
}
4

2 に答える 2

2

一度に複数の文字を読み書きします。16KB はおそらく妥当なバッファ サイズですが、自由に試してみてください。

于 2012-09-17T23:40:14.803 に答える
0

Buffer クラスBufferedInputStreamを使用して読み書きを試してください。BufferedOutputStream

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

幸運を

于 2012-09-18T00:33:01.590 に答える