Android练习之BitmapFactory.decodeFile加载SD卡文件

code

 

View Code
 1 package com.example.flexd13;
 2 
 3 import java.io.File;
 4 
 5 import android.os.Bundle;
 6 import android.app.Activity;
 7 import android.graphics.Bitmap;
 8 import android.graphics.BitmapFactory;
 9 import android.view.Menu;
10 import android.view.View;
11 import android.view.View.OnClickListener;
12 import android.widget.Button;
13 import android.widget.ImageView;
14 import android.widget.TextView;
15 
16 /**
17  * 点击Button给ImageView赋值
18  * 重点是从手机中找到图片,给Bitmap
19  */
20 public class MainActivity extends Activity {
21     
22     private TextView tv1;
23     private ImageView iv1;
24     private Button bt1;
25     private String fileName = "com.example.flexd13/baidu.gif";
26 
27     @Override
28     protected void onCreate(Bundle savedInstanceState) {
29         super.onCreate(savedInstanceState);
30         setContentView(R.layout.activity_main);
31         
32         tv1 = (TextView)findViewById(R.id.tv1);
33         iv1 = (ImageView)findViewById(R.id.iv1);
34         bt1 = (Button)findViewById(R.id.bt1);
35         
36         bt1.setOnClickListener(listener);
37         
38     }
39     
40     /**
41      * Listener
42      */
43     View.OnClickListener listener = new OnClickListener() {
44         
45         @Override
46         public void onClick(View v) {
47             File file = new File(fileName);
48             if(file.exists()){
49                 //从文件生成Bitmap
50                 Bitmap bitmap = BitmapFactory.decodeFile(fileName);
51                 iv1.setImageBitmap(bitmap);
52                 tv1.setText(fileName);
53             }else{
54                 tv1.setText("ERROR file not found");
55             }
56         }
57     };
58 
59     @Override
60     public boolean onCreateOptionsMenu(Menu menu) {
61         // Inflate the menu; this adds items to the action bar if it is present.
62         getMenuInflater().inflate(R.menu.activity_main, menu);
63         return true;
64     }
65 
66 }

 

转载于:https://www.cnblogs.com/imzzh/archive/2013/01/09/2852951.html

你可能感兴趣的:(Android练习之BitmapFactory.decodeFile加载SD卡文件)