tags:

views:

20

answers:

0

Hi,

Can someone please provide me some solutions as i get null from an image when i tried to retrieve it from an url. It only happens to some images.

Here are my codes:

image = ImageOperations(this.getBaseContext(), "http://example.download/fairylando_sihc2nck_2.jpg", "image.JPEG");

LinearLayout filepanel =  (LinearLayout) findViewById(R.id.galleryfile);
filepanel.setBackgroundDrawable(image);
if (image == null){
      Toast.makeText(getApplicationContext(),"Empty image",Toast.LENGTH_SHORT).show();
}
private Drawable ImageOperations(Context ctx, String url,String saveFilename) {
   try {
        InputStream is = (InputStream) this.fetch(url);
        Drawable d = Drawable.createFromStream(is, "src");
        is.close();
        return d;
   } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
   } catch (IOException e) {
   e.printStackTrace();
   return null;
 }
}
 public Object fetch(String address) throws MalformedURLException,IOException {
      URL url = new URL(address);
      Object content = url.getContent();
      return content;
 }