0

i want to load an image from a url.i have 10 images on my server and i m calling one in every class .it works but there are some pictures that are not opening...is there any wrong on my code?i have checked the url and its ok!thanks

public class modern_clock extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.sites); 



        ImageView im = (ImageView) findViewById(R.id.image); 
        Drawable drawable = LoadImageFromWeb("http://kostas-menu.gr/chania/roloi.jpg");
        im.setImageDrawable(drawable);



        TextView title1 = (TextView) findViewById(R.id.text_title);
        title1.setText("The Clock\n" );

    TextView info = (TextView) findViewById(R.id.info);
    info.setText(".................\n" );





    }
     private Drawable LoadImageFromWeb(String url) {
        // TODO Auto-generated method stub
         try{
         InputStream is = (InputStream) new URL(url).getContent();
           Drawable d = Drawable.createFromStream(is, "src name");
           return d;
          }catch (Exception e) {
           System.out.println("Exc="+e);
           return null;
    }
} 


}

EDIT:

@George:

i have added in my package the LoaderImageView,java class.i also changed my xml file from the simple imageview to

<com.example.android.LoaderImageView
    android:layout_marginTop="10px"
   android:id="@+id/loaderImageView"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
android:gravity="center"
   image="http://developer.android.com/images/dialog_buttons.png"
   />

and in every class of my project i have added that:

final LoaderImageView image = new LoaderImageView(this, "http://kostas-menu.gr/chania/santrivani.jpg");
        image.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

but unfortunately with that way i m always getting the image from the url in my xml file.i have the same xml for all my classes,so how could i set the image from the java file?i tried to erase the line image:"http...." from the manifest,but this is not returning any image at all!!thanks

kostas
  • 61
  • 5
  • have you tried loading from a local resource image? does it work? – ahmet alp balkan May 15 '11 at 13:43
  • yes from a raw folder for example is working ok!but i would like to load it from a url in order to have a smaller in size app!! – kostas May 15 '11 at 13:53
  • possible duplicate of [Load images from URL](http://stackoverflow.com/questions/6009092/load-images-from-url) – Kon May 15 '11 at 15:20
  • Is there a reason you're submitting another question about the same problem? – Kon May 15 '11 at 15:21
  • i have tried it with two ways,the first i posted and the second that @George told me to do.but in both wayas some pictures are not loading...all my pictures have the same dimensions and they are on the same server..whats going on?that drives me crazy:P – kostas May 15 '11 at 15:40

2 Answers2

1

Ideally you should fetch the images in a seperate thread different from the UI thread.

Lazy load of images in ListView

This might help too.

Community
  • 1
  • 1
Primal Pappachan
  • 25,857
  • 22
  • 67
  • 84
0

use this resources. I am sure this will help you

http://www.anddev.org/novice-tutorials-f8/imageview-with-loading-spinner-t49439.html

do you use this code? Is this something that we should?

public class ImageExampleXML extends Activity {

    private final String images[] = {"http://developer.android.com/images/dialog_custom.png", "http://developer.android.com/images/dialog_progress_bar.png"};
    private int i = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        final Button loadImage = (Button) findViewById(R.id.button);

        final LoaderImageView image = (LoaderImageView) findViewById(R.id.loaderImageView);

        loadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                image.setImageDrawable(getAnImageUrl());
            }
        });
    }

    private String getAnImageUrl() {
        i++;
        if(i >= images.length){
            i = 0;
        }
        return images[i];
    }
}
George
  • 1,327
  • 11
  • 25