Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fetch a large image from a url?

Tags:

android

I used the code below to fetch the image from a url but it doesn't working for large images.

Am I missing something when fetching that type of image?

imgView = (ImageView)findViewById(R.id.ImageView01);
  imgView.setImageBitmap(loadBitmap("http://www.360technosoft.com/mx4.jpg"));

  //imgView.setImageBitmap(loadBitmap("http://sugardaddydiaries.com/wp-content/uploads/2010/12/how_do_i_get_sugar_daddy.jpg"));
     //setImageDrawable("http://sugardaddydiaries.com/wp-content/uploads/2010/12/holding-money-copy.jpg");
  //Drawable drawable = LoadImageFromWebOperations("http://www.androidpeople.com/wp-content/uploads/2010/03/android.png");
  //imgView.setImageDrawable(drawable);

 /* try {
     ImageView i = (ImageView)findViewById(R.id.ImageView01);
     Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://sugardaddydiaries.com/wp-content/uploads/2010/12/holding-money-copy.jpg").getContent());
     i.setImageBitmap(bitmap); 
   } catch (MalformedURLException e) {
     System.out.println("hello");
   } catch (IOException e) {
    System.out.println("hello");
   }*/
    }


 protected Drawable ImageOperations(Context context, String string,
   String string2) {
  // TODO Auto-generated method stub
  try {
   InputStream is = (InputStream) this.fetch(string);
   Drawable d = Drawable.createFromStream(is, "src");
   return d;
  } catch (MalformedURLException e) {
   e.printStackTrace();
   return null;
  } catch (IOException e) {
   e.printStackTrace();
   return null;
  }

 }
like image 300
Kutbi Avatar asked Jan 01 '11 04:01

Kutbi


3 Answers

You're trying to download the Large Image from within the UI Thread....This will cause an ANR (Application not Responding)

Use AsyncTask to download the images, that way, a seperate thread will be used for the download and your UI Thread wont lock up.

like image 113
st0le Avatar answered Oct 04 '22 00:10

st0le


see this good example that loads image from server. blog.sptechnolab.com/2011/03/04/android/android-load-image-from-url/.

like image 41
ChandreshKanetiya Avatar answered Oct 04 '22 01:10

ChandreshKanetiya


If you want to download the image very quickly then you can use AQuery which is similar to JQuery just download the android-query.0.15.7.jar

Import the jar file and add the following snippet

 AQuery aq = new AQuery(this);   
 aq.id(R.id.image).image("http://4.bp.blogspot.com/_Q95xppgGoeo/TJzGNaeO8zI/AAAAAAAAADE/kez1bBRmQTk/s1600/Sri-Ram.jpg");   

// Here R.id.image is id of your ImageView 
// This method will not give any exception 
// Dont forgot to add Internet Permission
like image 45
KK_07k11A0585 Avatar answered Oct 04 '22 01:10

KK_07k11A0585