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

时间:2016-08-27 13:15:35

标签: android image gallery rename

我制作了一个自定义图库,我可以在其中选择多个图像,然后执行以下步骤:

  1. 通过文件I / O

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

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

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

1 个答案:

答案 0 :(得分:0)

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

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