第一种: //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(); } } }
第二种方式有个坑,就是在复制的时候图片的尺寸会变原因我还没找到,第一种就没有这个问题