How to delete an image?

edited September 2017 in Questions about Code

Dear friends, I have designed an image and save it in my sketch folder. I want to delete this image from the sketch folder after an event such as mouse pressed. Can anyone suggest me how to delete an image from sketch?

Answers

  • I googled "delete file site: processing.org" and found this as my top result: https://forum.processing.org/one/topic/noob-how-to-delete-a-file-in-the-data-folder.html

  • edited September 2017

    Yes, but it is not working... did you typed the code present in the link? I have checked it. Try it your self:

    import java.io.File;
    String fileName;
    void setup() {
      size(480, 360);
      background(0);
      ellipse(width/2, height/2, width/2, height/2);
      fill(255);
      save("image1.jpg");
    }
    
    void mousePressed() {
      fileName = dataPath("image1.jpg");
      File f = new File(fileName);
      if (f.exists()) {
        f.delete();
      }
    }
    

    It is not working.

  • save("image1.jpg");
    

    this saves in the sketch folder, not the data folder. you are deleting from the data folder...

    also, without a draw your code will finish after setup so there won't be time to press a key...

    import java.io.File;
    
    String fileName;
    void setup() {
      size(480, 360);
      background(0);
      ellipse(width/2, height/2, width/2, height/2);
      fill(255);
      save(dataPath("image1.jpg")); // save in datapath
    }
    
    void draw() {
    }
    
    void mousePressed() {
      fileName = dataPath("image1.jpg");
      File f = new File(fileName);
      if (f.exists()) {
        println("deleting");
        f.delete();
      }
    }
    
  • edited September 2017 Answer ✓
    // Forum.Processing.org/two/discussion/24014/how-to-delete-an-image#Item_4
    // GoToLoop (2017-Sep-03)
    
    static final String IMG_NAME = "shot.png";
    
    void setup() {
      size(400, 400);
      smooth(3);
      noLoop();
    
      blendMode(REPLACE);
      ellipseMode(CENTER);
    
      strokeWeight(3.5);
      stroke(-1);
    }
    
    void draw() {
      clear();
      fill((color) random(#000000));
      ellipse(width>>1, height>>1, width>>1, height>>1);
      saveFrame(dataPath(IMG_NAME));
    }
    
    void mousePressed() {
      if (mouseButton != CENTER)  dataFile(IMG_NAME).delete();
      else                        redraw = true;
    }
    
  • Thanks...

Sign In or Register to comment.