problem with catch whyle downloading image from url and save to disk
in
Programming Questions
•
4 months ago
Hy,
since two days I work on a small piece of code that crashes all the time. I placed try and catch events, tried to simplify it … but it does not help.
My code shall download and save images from websites. Therefore it analyzes the html code and searches for jpg' s. As far as well… The problem since to be when images are not available or not completely downloaded when the save event is called.
Can anybody help me? I am getting grey hairs…
- //Image Parser
- String [] url = {
- "http://www.elclarin.cl/web",
- "http://www.elciudadano.cl",
- "http://www.elmostrador.cl"
- };
- String [] SiteHTML = new String [0];
- void setup() {
- size(10, 10, P2D); //Full HD = 1929 * 1080 //
- frameRate(10);
- background(0);
- noLoop();
- }
- void draw() {
- for (int j=0; j<url.length; j++) {
- try {
- SiteHTML = loadStrings (url[j]); //--load HTML Code
- }
- catch(Exception e) {
- e.printStackTrace();
- println("Access on "+url[j] +" denied "+e);
- }
- if (SiteHTML != null) {
- for (int i=0; i< SiteHTML.length; i++) {
- //--------
- if ( SiteHTML[i].indexOf("<img") > -1 ) {
- String temp1 [] = split(SiteHTML[i], "<img");
- for (int k=0; k< temp1.length; k++) {
- if ( temp1[k].indexOf("jpg") > -1) { // just look for jpg images
- //println( temp1[k]);
- String temp2 = split(temp1[k], "src=\"")[1]; // delete code bevore the image's adress
- temp2 = split(temp2, "\"")[0]; // delete code after the image's adress
- if (temp2.indexOf("http://") < 0 ) temp2 = url[j] + "/" + temp2; //if the image adress is a path on the server
- if ( temp2.indexOf("jpg") > -1 && temp2.indexOf("http://") == 0 ) { //just to be shure to have a real adress
- PImage image = null;
- try {
- image = loadImage(temp2);
- delay(200);
- if (image == null) println("ACHTUNG !!! "+ temp2);
- }
- catch(Exception e) {
- }
- if (image != null && image.width * image.height > 4000) { //don't save too small images
- try {
- image.save("Data/Images/"+j+"-"+i+".jpg");
- }
- catch(Exception e) {
- e.printStackTrace();
- println("Speichern fehlgeschlagen "+e);
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
1