如何以编程方式将图像从一个文件夹复制到另一个文件夹时提高性能

How to improve performance while copying an image from one folder to another programmatically

我创建了一个自定义画廊,我可以在其中 select 多张图片,然后执行以下步骤:

  1. 通过文件I/O

  2. 将该图像复制到另一个文件夹
  3. 在传输到目标文件夹时重命名图像

虽然我可以做这两件事,但两者之间有很长的延迟。

Calendar calendar = Calendar.getInstance();
                Bundle bundle = new Bundle();
                for (int j=0;j<mTempFiles.size();j++){
                    try {
                        String path = mTempFiles.get(j);
                        String destination = Environment.getExternalStorageDirectory() + "/BOM/";
                        File imgDest = new File(destination,"32_"+"Product" + new Date().getTime() + calendar.get(Calendar.DATE) + calendar.get(Calendar.MONTH) + calendar.get(Calendar.YEAR)  + ".png");
                        imgDest.createNewFile();

                        outputStream = new FileOutputStream(imgDest);

                        File out = new File(path);
                        FileInputStream fis = new FileInputStream(out);
                        Bitmap imgBitmap = BitmapFactory.decodeStream(fis);

                        ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
                        imgBitmap.compress(Bitmap.CompressFormat.JPEG,100,byteOutput);
                        final byte[] imgBytes = byteOutput.toByteArray();
                        outputStream.write(imgBytes);
                        outputStream.close();

                        String dest = imgDest.getAbsolutePath();
                        compressImage(dest);
                        Bitmap bitmap = BitmapFactory.decodeFile(dest);
                        mPath.add(dest);
                        Constants.mCategoryModels.add(new CategoryModel(bitmap));

                        Constants.mFiles.add(imgDest);
                        Log.e("Main Array Size:----  ",""+Constants.mFiles.size());

这是复制文件的一种方法:

private static void copyFile(File source, File dest) throws IOException {
    InputStream is = null;
    OutputStream os = null;
    try {
        is = new FileInputStream(source);
        os = new FileOutputStream(dest);
        byte[] buffer = new byte[4096];
        int length;
        while ((length = is.read(buffer)) > 0) {
            // do stuff on buffer
            os.write(buffer, 0, length);
        }
    } finally {
        is.close();
        os.close();
    }
}

/* use it as follows */

File fileToCopy = new File(path);
File destinationFile = new File(path2);
copyFile(fileToCopy, destinationFile);