processing-android
processing-android copied to clipboard
requestImage crashes app when URL file cannot be found
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 /**
- Call createInput() without automatic gzip decompression. */ public InputStream createInputRaw(String filename) { // Additional considerations for Android version: // http://developer.android.com/guide/topics/resources/resources-i18n.html InputStream stream = null;
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); } }
@ajavamind Thanks for bringing this up and suggesting a fix. I will tag it for inclusion in the next release.