I need to load an image from a web in a simple Java stand alone aplication. Any Ideas?
You can load an image using
BufferedImage img = ImageIO.read(new URL("http://stackoverflow.com/content/img/so/logo.png"));
For methods how to display the loaded image, see the Sun "Working with images" tutorial.
URL url = new URL("http://host/theimage.jpg");
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
is that enough to start you? Don't know what you want to do from there.
See the documentation at ImageIO.read(URL).
I would take a look at HTTPClient.
Find the URL to the image, and you can get an inputstream feeding you the image data, plus you'll get the content-type etc. so you can correctly handle it once you've downloaded it.
Here's some sample code. You may also need to call getResponseHeaders() on the GetMethod to identify the image type.
Related
I am building an Android Things "kiosk" style application and I want an image to show in the background like a wallpaper. I'm using Unsplash Source to get a random image but the source URL (https://source.unsplash.com/1080x1920/?long-exposure) always redirects to an image URL, so I can't use this:
InputStream is = (InputStream) new URL("https://source.unsplash.com/1080x1920/?long-exposure").getContent();
Drawable d = Drawable.createFromStream(is, "");
niceWallpaper.setImageDrawable(d);
Is there a way to do this?
Using Picasso can solve your problem.
String yourUrl = "https://source.unsplash.com/1080x1920/?long-exposure";
Picasso.with(MyApplication.getAppContext()).load(yourUrl).placeholder(defaultImage).memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE).into(YourImageView);
Use Picasso.
Picasso.get().load("https://source.unsplash.com/1080x1920/?long-exposure")
.into(imageView);
I have been wrestling with this all night . Here's the issue I am using the google places api in my android project and want to use the icons provided in the json output. I have looked at the questions already posed and they all refer to javascript. Essentially in the log file it keeps stating that it can't find the file when I attempt to access this path. I can put the uri in the browse and it show up any ideas?
the uri is http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png and heres the code it gives a warning too and when the marker is added (because its null errors out)
private Bitmap getBitmapFromUri(Uri uri) throws IOException {
AssetFileDescriptor FDescriptor =
//getContentResolver().openFileDescriptor(uri, "r");
getContentResolver().openAssetFileDescriptor(uri, "r");
FileDescriptor fileDescriptor = FDescriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
FDescriptor.close();
return image;
}
any help would be greatly appreciated
See the documentation for openAssetFileDescriptor. It doesn't handle the http scheme. You will probably either want to download this file manually and then use a method like the above to load a local file or use a library like Picasso, which will download and cache the image for you (and even load it into your ImageView).
I'm currenty developing for blackberry and just bumped into this problem as i was trying to download an image from a server. The servlet which the device communicates with is working correctly, as I have made a number of tests for it. But it gives me the
413 HTTP error ("Request entity too large").
I figure i will just get the bytes, uhm, portion by portion. How can i accomplish this?
This is the code of the servlet (the doGet() method):
try {
ImageIcon imageIcon = new ImageIcon("c:\\Users\\dcalderon\\prueba.png");
Image image = imageIcon.getImage();
PngEncoder pngEncoder = new PngEncoder(image, true);
output.write(pngEncoder.pngEncode());
} finally {
output.close();
}
Thanks. It's worth mentioning that I am developing both the client-side and the server-side.
I am not aware by server side code. You can look on this Link to get an idea how to upload file using multipart to support Big files uploading
it can also work on blackberry , With some modifications needed.
http://www.developer.nokia.com/Community/Wiki/HTTP_Post_multipart_file_upload_in_Java_ME
I'm not familiar with the PNGEncoder class you're using, but just looking at your servlet code, and the comment you made about the request size (2.2 MB), I'm guessing that part of your problem is that you're uncompressing the image, and then transmitting it across the network.
I don't think you should have any PNGEncoder or ImageIcon code in your servlet. You should just read the "c:\\Users\\dcalderon\\prueba.png" file in with a normal InputStream as bytes, and then write that to the servlet's output. I don't think it matters whether that file is a PNG image, a .mp3 file, or any other content. (although you might need to set the Content Type to image/png).
So, I would try transmitting the image compressed (as a .png just as it's stored on disk). If that still doesn't work, then go with the suggestion to use multipart transmission.
I have a byte[] of an image and I need to upload it as an image to picasa.
According to the documentation, an image is uploaded as follows.
MediaFileSource myMedia = new MediaFileSource(new File("lights.jpg"), "image/jpeg");
which means I need to create a File, out of the byte[].
The catch is, I have to do this without using FileOutputStream as it is not supported by Google App Engine (which is the environment I am using)
Is there any way to do this?
You don't have to use MediaFileSource to upload a photo, you can use MediaByteArraySource and pass it to photo.setMediaSource(...).
I am new in Java programming. My query is that I am having an image which is present on a server and I want to display that image inside the JFrame. I tried using the Image class but that seems to be not working.
Please Note: I don't want to use applets for this, so is there some other method by which this can be done?
Thanks & Regards,
Assuming that it's a public accessible webserver, you can use URL#openStream() to get an InputStream out of an URL.
InputStream input = new URL("http://example.com/image.png").openStream();
Then you can just create the BufferedImage with help of ImageIO#read() the usual way.
BufferedImage image = ImageIO.read(input);
// ...