两种复制图片的方式比较

第一种:


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

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

转载于:https://my.oschina.net/lengwei/blog/684305

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值