开发者

Problem on loading images from url

开发者 https://www.devze.com 2023-03-05 20:13 出处:网络
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 ch

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_c开发者_StackOverflow社区ontent"
   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


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.


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];
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号