I want to download an image from an android app. However, in the app I don't know the name of the image.
In a php script, I choose the image file that the app should download randomly and now I have two options:
What is the better way? And if it is the first, could you link to a good page on how to download an image? I can upload images easily but the internet is full of different ways on how to download an image, so I don't know which one is the best...
The way i did it was the first way, the image URL was stored in a database and retrieved through json along with other data, the i used the below code to get the image from the URL.
public class ImageDownloader {
public static Bitmap downloadImage(String url) throws MalformedURLException{
return downloadImage(new URL(url));
}
public static Bitmap downloadImage(URL url){
Bitmap bmImg = null;
try {
HttpURLConnection conn= (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return bmImg;
}
}
It returns bitmap
public static Bitmap getImage(String url){
Bitmap img = null ;
try {
URL feedImage = new URL(url);
HttpURLConnection conn= (HttpURLConnection)feedImage.openConnection();
InputStream is = conn.getInputStream();
img = BitmapFactory.decodeStream(is);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return img ;
}