0

我有错误代码帮助我修复它,单击按钮后从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;
}

}

活动主文件.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 的图像后显示

4

2 回答 2

0

您试图将 String 设置为Imageview的背景。您需要从 URL 加载图像。阅读本教程以从 URL 获取图像或查看此答案

于 2013-08-25T07:20:18.007 回答
0
  1. 首先,您必须从这里下载 Android 查询 jar 。

    Android 查询很容易下载或从基于 AJAX 调用的 URL 加载图像。

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

  3. 活动代码

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;
    }
于 2013-09-05T11:55:50.930 回答