【问题标题】:How to improve performance while copying an image from one folder to another programmatically如何在以编程方式将图像从一个文件夹复制到另一个文件夹时提高性能
【发布时间】:2017-01-04 00:38:53
【问题描述】:

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

  1. 通过文件 I/O 将该图像复制到另一个文件夹

  2. 在传输到目标文件夹时重命名图像

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

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

【问题讨论】:

  • 你能告诉我们你的复制文件的代码吗?
  • 我已经添加了代码请看一下@A.Omar
  • 为什么你的代码会涉及到一些位图操作?您只想复制现有文件并重命名它,对吗?
  • 我在将图像移动到目的地之前对其进行压缩..

标签: android image gallery rename


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 2018-03-18
    • 2020-07-30
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多