- Forum posts: 21
Nov 16, 2014, 12:34:28 PM via Website
Nov 16, 2014 12:34:28 PM via Website
Hi everyone, I'm trying to create an application that on one of its activities it downloads images from a remote server to the user's device. These are the functions I use:
private void getPicture() {
Bitmap bitmap = DownloadImage("http://ibuy.pixub.com/ibuy/files/Pic5.jpg");
ImageView img = (ImageView) findViewById(R.id.imageView);
img.setImageBitmap(bitmap);
}
private Bitmap DownloadImage(String URL) {
Bitmap bitmap = null;
InputStream in;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}
private InputStream OpenHttpConnection(String urlString) throws IOException {
InputStream in = null;
int response;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK)
in = httpConn.getInputStream();
} catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
My application falls on this line: throw new IOException("Error connecting");.
The path to the image is fine and I can see the image if I type it in the browser. Do I need to use a PHP file? Do I need just to change something in my code? Thank you in advance!
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.