xiantu.java
public class xiantu extends Activity { /*声明对象变量*/ private ImageView mImageView; private Button mButton; private TextView mTextView; private String fileName="/data/data/irdc.ex04_22/ex04_22_2.png"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 载入main.xml Layout */ setContentView(R.layout.main); /* 取得Button对象,并添加onClickListener */ mButton = (Button)findViewById(R.id.mButton); mButton.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { /* 取得对象 */ mImageView = (ImageView)findViewById(R.id.mImageView); mTextView=(TextView)findViewById(R.id.mTextView); /* 检查文件是否存在 */ File f=new File(fileName); if(f.exists()) { /* 产生Bitmap对象,并放入mImageView中 */ Bitmap bm = BitmapFactory.decodeFile(fileName); mImageView.setImageBitmap(bm); mTextView.setText(fileName); } else { mTextView.setText("此文件不存在"); } } }); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android:id="@+id/layout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/white" xmlns:android="http://schemas.android.com/apk/res/android" > <TextView android:id="@+id/mTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/str_text" android:layout_x="30px" android:layout_y="30px" android:textColor="@drawable/blue" > </TextView> <ImageView android:id="@+id/mImageView" android:layout_width="250px" android:layout_height="188px" android:src="@drawable/example060" android:layout_x="30px" android:layout_y="62px" > </ImageView> <Button android:id="@+id/mButton" android:layout_width="155px" android:layout_height="wrap_content" android:text="@string/str_button" android:textSize="18sp" android:layout_x="80px" android:layout_y="302px" > </Button> </AbsoluteLayout>