我在显示图片时遇到问题,尝试使用onclick方法,gettext在edittext上输入文本后仍然无法显示图片。
这是我的代码
活动第一代码java
public class MainActivity extends Activity {
EditText et;
private Button tombol;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tombol=(Button)findViewById(R.id.button1);
tombol.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
intent.putExtra("URL", "http://epiece.hostingforfree.com/cam_1.jpg");
startActivityForResult(intent, 0);
}
});
}
第二个活动java代码
public class MainActivity2 extends Activity {
EditText et;
ImageView iv;
public Button tombol;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
loadGambar();
}
private void loadGambar(){
try {
iv = (ImageView) findViewById(R.id.imageView1);
et = (EditText) findViewById(R.id.editText1);
tombol = (Button) findViewById(R.id.button1);
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();
}
tombol=(Button)findViewById(R.id.button1);
tombol.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String string = getIntent().getExtras().getString("URL");
Intent intent=new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
此代码没有任何错误,但图像仍然无法显示,请帮我修复它?