Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

requestImage crashes app when URL file cannot be found #727

Open
ajavamind opened this issue Feb 15, 2023 · 1 comment
Open

requestImage crashes app when URL file cannot be found #727

ajavamind opened this issue Feb 15, 2023 · 1 comment

Comments

@ajavamind
Copy link

ajavamind commented Feb 15, 2023

I came across this error and found a solution.

Here is test code to create the problem

///////////////////////////////////////////////////
// requestImage test
PImage img;
boolean go = true;

void setup() {
size(800, 600);
}

void draw() {
if (go) {
//img = requestImage("http://192.168.0.112/DCIM/100PHOTO/SAM_0001.JPG"); // Loads the image from server successfully
img = requestImage("http://192.168.0.112/DCIM/PHOTO/SAM_0001.JPG"); // App crashes with exception
// java.lang.IllegalArgumentException: File http://192.168.0.112/DCIM/PHOTO/SAM_0001.JPG contains a path separator
go = false;
}
float ar = (float)img.width/(float)img.height;
if (img != null && img.width > 0 && img.height > 0) {
image(img, 0, 0, width, (float)width/ar);
} else {
text("Error ", width/2, height/2);
}
}
void mousePressed() {
go = true;
}
///-----------------------------------------------------------------

Process: processing.test.requestimagetest, PID: 8066
java.lang.IllegalArgumentException: File http://192.168.0.112/DCIM/PHOTO/SAM_0001.JPG contains a path separator
at android.app.ContextImpl.makeFilename(ContextImpl.java:2484)
at android.app.ContextImpl.getFileStreamPath(ContextImpl.java:699)
at android.content.ContextWrapper.getFileStreamPath(ContextWrapper.java:216)
at android.content.ContextWrapper.getFileStreamPath(ContextWrapper.java:216)
at processing.core.PSurfaceNone.getFileStreamPath(PSurfaceNone.java:319)
at processing.core.PApplet.sketchPath(PApplet.java:5506)
at processing.core.PApplet.createInputRaw(PApplet.java:5031)
at processing.core.PApplet.createInput(PApplet.java:4863)
at processing.core.PApplet.loadImage(PApplet.java:4003)
at processing.core.PApplet$AsyncImageLoader.run(PApplet.java:4076)

In PApplet.java createInputRaw() null is not returned when the file cannot be found causing expectation for non-URL filename

line 4879
/**

if (filename == null) return null;

if (filename.length() == 0) {
  // an error will be called by the parent function
  //System.err.println("The filename passed to openStream() was empty.");
  return null;
}

// safe to check for this as a url first. this will prevent online
// access logs from being spammed with GET /sketchfolder/http://blahblah
if (filename.indexOf(":") != -1) {  // at least smells like URL
  try {
    // Workaround for Android bug 6066
    // http://code.google.com/p/android/issues/detail?id=6066
    // http://code.google.com/p/processing/issues/detail?id=629

// URL url = new URL(filename);
// stream = url.openStream();
// return stream;
URL url = new URL(filename);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setDoInput(true);
con.connect();
return con.getInputStream();
//The following code is deprecaded by Android
// HttpGet httpRequest = null;
// httpRequest = new HttpGet(URI.create(filename));
// HttpClient httpclient = new DefaultHttpClient();
// HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
// HttpEntity entity = response.getEntity();
// return entity.getContent();
// can't use BufferedHttpEntity because it may try to allocate a byte
// buffer of the size of the download, bad when DL is 25 MB... [0200]
// BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
// return bufHttpEntity.getContent();

  } catch (MalformedURLException mfue) {
    // not a url, that's fine

**************************may need return null; // if it is really a URL it needs return null
} catch (FileNotFoundException fnfe) {
// Java 1.5 likes to throw this when URL not available. (fix for 0119)
// http://dev.processing.org/bugs/show_bug.cgi?id=403
**************************needs return null; // because this URL could not find the file, go no further
} catch (IOException e) {
// changed for 0117, shouldn't be throwing exception
printStackTrace(e);
//System.err.println("Error downloading from URL " + filename);
return null;
//throw new RuntimeException("Error downloading from URL " + filename);
}
}

@codeanticode
Copy link
Member

@ajavamind Thanks for bringing this up and suggesting a fix. I will tag it for inclusion in the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants