【问题标题】:Copying png file from a folder to another将png文件从一个文件夹复制到另一个文件夹
【发布时间】:2011-08-22 14:52:34
【问题描述】:

我的文件夹中有一个 PNG 文件,我想将此文件复制到另一个文件夹。有没有简单的方法来做到这一点?

例子:

//Creating PNG 

              File file = new File(Environment.getExternalStorageDirectory()
                + File.separator+"/S_Temp/temp_"+formattedDate+".png");

              FileOutputStream  out = new FileOutputStream(file);
              view.mBitmap.compress(Bitmap.CompressFormat.PNG,100, out);

所以,我在 SD 卡中的文件夹“S_Temp”中有 PNG,现在我想将此文件复制到 SD 卡本身的新文件夹中,例如“S”。

提前致谢

快乐编码

【问题讨论】:

    标签: android file copying


    【解决方案1】:
    private static void copyfile(String srFile, String dtFile){
    try{
      File f1 = new File(srFile);
      File f2 = new File(dtFile);
      InputStream in = new FileInputStream(f1);
    
      //For Append the file.
      //OutputStream out = new FileOutputStream(f2,true);
    
      //For Overwrite the file.
      OutputStream out = new FileOutputStream(f2);
    
      byte[] buf = new byte[1024];
      int len;
      while ((len = in.read(buf)) > 0){
        out.write(buf, 0, len);
      }
      in.close();
      out.close();
      System.out.println("File copied.");
    }
    catch(FileNotFoundException ex){
      System.out.println(ex.getMessage() + " in the specified directory.");
      System.exit(0);
    }
    catch(IOException e){
      System.out.println(e.getMessage());      
    }
    }
    

    试试这个。

    【讨论】: