Search code examples
androidhandlerandroid-imageviewrunnable

how to runtime image and get new image use handler


How i can runtime and get new image, i newbie on android, i am using eclipse for this project, i don't know how to make it this runnable, can somebody tell me how to get it ?

full code Main Activity:

Handler handler = new Handler();

EditText inputUrl;
OnClickListener getImageBtnOnClick = new OnClickListener() {
    public void onClick(View view) {
        Context context = view.getContext();
        Editable ed = inputUrl.getText();
        Drawable image = ImageOperations(context,ed.toString(),"image.jpg");
        ImageView imgView = new ImageView(context);
        imgView = (ImageView)findViewById(R.id.imageView1);
        imgView.setImageDrawable(image);
        Handler handler = new Handler(); 
        handler.postDelayed(new Runnable() { 
                 public void run() { 
                 } 
            }, 10000);     

    }
};

========================================================================================

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    inputUrl = ((EditText)findViewById(R.id.editText1));
    inputUrl.setSingleLine();
    inputUrl.setTextSize(11);
    Button getImageButton = (Button)findViewById(R.id.button1);
    getImageButton.setOnClickListener(getImageBtnOnClick);
}
private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
    try {
        InputStream is = (InputStream) this.fetch(url);
        Drawable d = Drawable.createFromStream(is, "src");
        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;
}

activity_main.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="18dp"
    android:ems="10" />

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText1"
    android:text="Button" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/button1"
    android:layout_marginTop="21dp"
    android:src="@drawable/ic_action_search" />


Solution

  • this is your problem

    final ImageView imgView = new ImageView(context);