ImageView使用和从内存读取图片显示

 1 import java.io.File;  
2
3 import android.app.Activity;
4 import android.graphics.Bitmap;
5 import android.graphics.BitmapFactory;
6 import android.os.Bundle;
7 import android.view.View;
8 import android.widget.Button;
9 import android.widget.ImageView;
10 import android.widget.TextView;
11
12 public class ImageViewActivity extends Activity {
13 private Button myButtonSwitchImg;
14 private ImageView myImageView;
15 private TextView myTextView;
16 private String filename = "/data/data/img.bmp";
17 @Override
18 public void onCreate(Bundle savedInstanceState) {
19 super.onCreate(savedInstanceState);
20 setTitle("ImageView");
21 setContentView(R.layout.imageview);
22 myButtonSwitchImg = (Button) findViewById(R.id.imgButton);
23 myImageView = (ImageView) findViewById(R.id.imageView);
24 myTextView = (TextView) findViewById(R.id.imgText);
25
26 myButtonSwitchImg.setOnClickListener(new Button.OnClickListener() {
27 @Override
28 public void onClick(View v) {
29 File file = new File(filename);
30 if(file.exists()) {
31 Bitmap bm = BitmapFactory.decodeFile(filename);
32 myImageView.setImageBitmap(bm);
33 myTextView.setText(filename);
34 } else {
35 myTextView.setText("File Not Found!");
36 }
37 }
38 });
39 }
40 }

你可能感兴趣的:(imageview)