侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130555 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

两种复制图片的方式比较

2024-05-10 星期五 / 0 评论 / 0 点赞 / 6 阅读 / 1891 字

第一种: //oldPath是文件所在路径,newPath是新文件的路径 File file = new File(oldPath); if (file.exists()){

第一种:

    //oldPath是文件所在路径,newPath是新文件的路径    File file = new File(oldPath);        if (file.exists()){            try {                int byteRead = 0;                InputStream inputStream = new FileInputStream(oldPath);                FileOutputStream fos = new FileOutputStream(newPath);                byte [] buffer = new byte[1444];                while((byteRead = inputStream.read(buffer)) != -1){                    fos.write(buffer,0,byteRead);                }                inputStream.close();                fos.close();            } catch (FileNotFoundException e) {                e.printStackTrace();            } catch (IOException e){                e.printStackTrace();            }        }else{            throw new FileNotFoundException("原始文件不存在");        }    }

第二种:

    ByteArrayOutputStream out = new ByteArrayOutputStream();    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.piantou1);    bitmap.compress(Bitmap.CompressFormat.JPEG,100,out);    for (int i = 1; i < 61; i++) {            byte [] buffer = out.toByteArray();            try {                File file = new File(getSDCardPath()+"copy"+File.separator+"piantou"+i+".jpg");                OutputStream ou = new FileOutputStream(file);                try {                    ou.write(buffer);                    ou.flush();                    ou.close();                } catch (IOException e) {                    e.printStackTrace();                }            } catch (FileNotFoundException e) {                e.printStackTrace();            }        }    }

第二种方式有个坑,就是在复制的时候图片的尺寸会变原因我还没找到,第一种就没有这个问题

广告 广告

评论区