Easiest way to download an image from a direct URL?

2 views
Skip to first unread message

Dan Watling

unread,
Oct 17, 2009, 2:03:39 PM10/17/09
to PulpCore
Has anyone come across the need to download an image from a URL and
not have it in the asset catalog file? For instance, I would like to
grab an image from Facebook and incorporate it into my applet. Can I
just do something like this:

image = new CoreImage("http://facebok.com/<whatever_i_need_here>")

If not, would this be a good addition to CoreImage? Where should it
be?

-Dan

Troy Cox

unread,
Oct 17, 2009, 2:35:12 PM10/17/09
to PulpCore

Jonathan Chung

unread,
Oct 17, 2009, 5:45:01 PM10/17/09
to PulpCore
I don't think that that approach would work because I'm not sure if
applets are allowed to pull data from different servers (at least not
till 6u10).

One thing you could do, which is a little roundabout is call a server-
side script that fetches the image, reports the bits back to you and
dumps that into an asset catalog.

Let me know if this helps.

Troy Cox

unread,
Oct 17, 2009, 6:46:56 PM10/17/09
to PulpCore
I believe Jonathan is correct: http://www.duckware.com/applets/reference.html

Dan Watling

unread,
Oct 18, 2009, 9:53:30 AM10/18/09
to PulpCore
Good idea about the "roundabout" method. I will have a go at that once
I get my applet deployed to my server.

The link was very informative too, although I've been doing my testing
locally and have been able to access images off of other websites
(e.g. facebook, and a few others) without problem. The article
mentioned local testing would only be able to pull images locally.
Perhaps I read it wrong, or maybe there is a setting somewhere I don't
know about that allows me to do this. Regardless though, I know I'll
have to do something once I deploy my applet.

This probably isn't perfect, or even recommended, code. But maybe
it'll help someone else out while their learning. Here's the code I
wrote to download from a URL -- not sure if it works on a deployed
applet as, again, I've only been working local at this point:

public static CoreImage downloadImage(String urlStr) {
ByteArray in = null;
Download d = Download.startDownload(urlStr);
while (d.getState() == Download.DOWNLOADING) {
// wait until we're done downloading
}

in = new ByteArray(d.getData());

in.reset();
return CoreSystem.getThisAppContext().loadImage(in);
}

-Dan

Dori

unread,
Oct 18, 2009, 10:24:00 AM10/18/09
to PulpCore
Keep in mind is that if your testing your applets locally, say via the
pulpcore applet viewer, that the same security contraints do not apply
to running an applet in-browser, the important implication of this for
you i image is that while running the applet on your own machine out
of browser you applet should be able to access any remote address,
while in browser this would be limated to the host for which the the
applet was downloaded from (unless the applet has been granted any
special permissions)

Dori

Troy Cox

unread,
Oct 18, 2009, 4:51:00 PM10/18/09
to PulpCore
Dan,

I know you can figure this out.
I wanted to have a little fun with an interesting experiment ;)

The sample:

http://getgamesforfree.com/pulpcore/get_data/

The code:

Using the 'Hello World' example as a starter, I altered the update
method in the title scene as follows:

I comment out: //addEvent(new SceneChangeEvent(new HelloWorld(),
300));

and replaced it with:

CoreImage img = null;
try {
img = UrlTest.GetUrlImage("http://www.interactivepulp.com/images/
logo.png") ;
} catch (Exception e) {
e.printStackTrace();
}
Sprite test = new ImageSprite(img,320,300);
test.setAnchor(.5,.5);
add(test);


img is set by a static method in UrlTest.

Then, in the UrlTest class, I added your code:

public class UrlTest {

public static CoreImage GetUrlImage(String urlStr) {
ByteArray in = null;
Download d = Download.startDownload("http://myWebSite.com/
get_data.php?url="+urlStr);
while (d.getState() == Download.DOWNLOADING) {
// wait until we're done downloading
}

in = new ByteArray(d.getData());

in.reset();
return CoreSystem.getThisAppContext().loadImage(in);
}
}

That's pretty much your code, but passing the desired urlStr to
get_data.php in a folder accessible to the applet.

Then, put this in the get_data.php

<?php
/* gets the data from a URL */
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo get_data($_GET["url"]);
?>

That's fairly standard. I googled it--tweaked it.

It just echos the data. There isn't technically a download. So,
there's never a file on your server.

A php like this is a very interesting little critter open to a lot of
potential for abuse.
It will relay pretty much anything.

I'd add some security to only accept from your applet.

Dan Watling

unread,
Oct 19, 2009, 1:46:46 PM10/19/09
to PulpCore
Wow! Thanks for the code. It'll save me some time at least to get
things going.

I wonder if something like this would be useful to incorporate into
Pulpcore. Maybe there could be an text file in the assets catalog with
a list of external images / files to download automatically in the
LoadingScene.

-Dan
Reply all
Reply to author
Forward
0 new messages