在输入文本URL和按下按钮后显示图像使用

时间:2013-08-25 06:05:44

标签: android android-layout imageview android-edittext imagebutton

我有错误代码帮我修复它,在点击按钮后从edittext获取文本URL并在imageView上显示图像,

MainActivity.java

public class MainActivity extends Activity {

Button bt;
EditText et;
ImageView iv;
Runnable stream;

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    loadGambar();

    stream = new Runnable() {



@Override
        public void run() {
            try {
                loadGambar();
            } catch (Exception e) {
                e.getStackTrace();
            }
            new Handler().postDelayed(stream, 2);
        }
    };
    new Handler().postDelayed(stream, 2);
}

private void loadGambar(){
    try {
        iv = (ImageView) findViewById(R.id.imageView1);
        et = (EditText) findViewById(R.id.editText1);
        bt = (Button) findViewById(R.id.button1);



bt.setOnClickListener(new view.OnClickListener() {  
            public void onClick(View view) {
                    et   = (EditText)findViewById(R.id.editText1);
                    iv = (ImageView)findViewById(R.id.imageView1);
                    iv.setBackground(et.getText().toString());

            }
        });



URL url = new URL (et.getText().toString());
        InputStream is =  (InputStream) url.getContent();
        Drawable gambar = Drawable.createFromStream(is, "src");
        iv.setImageDrawable(gambar);
        is.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

acitvity_main.xml

<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" android:inputType="textUri">

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="21dp"
    android:text="@string/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="18dp"
    android:src="@drawable/ic_action_search" android:contentDescription="@string/app_name"/>

如何在输入URL后显示edittext并按下按钮以显示来自URL的图像

2 个答案:

答案 0 :(得分:0)

您尝试将String设置为 Imageview 的背景。您需要从URL加载图像。阅读此tutorial以从网址中提取图片或查看此answer

答案 1 :(得分:0)

  1. 首先,您必须从here下载Android查询jar。

    可以轻松下载Android查询,也可以根据AJAX调用从URL加载图片。

  2. 将jar添加到项目libs文件夹并将jar添加到构建路径。

  3. 活动代码

  4. public class MainActivity extends Activity {
    
    Button bt;
    EditText et;
    ImageView iv;
    
    private AQuery androidQuery;
    
    @Override
     public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    bt = (Button) findViewById(R.id.bt);
    et = (EditText) findViewById(R.id.et);
    iv = (ImageView) findViewById(R.id.imageView1);
    androidQuery = new AQuery(this);
    
    bt.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View arg0) {
            if(et.getText().toString().trim().length()>0){
            // this code load image form url
             androidQuery.image(et.getText().toString(), true, true); 
                                    /*or*/
             // this code download image
             androidQuery.ajax(et.getText().toString(), Bitmap.class, 0, new AjaxCallback<Bitmap>() {
                    @Override
                    public void callback(String url, Bitmap object, AjaxStatus status) {
                        super.callback(url, object, status);
                        if(object!=null){
                            iv.setImageBitmap(object);
                        }else{
                            // write message if give path image not found
                        }
                    }
                });
            }
        }
    });
    }
    
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
         getMenuInflater().inflate(R.menu.activity_main, menu);
          return true;
        }