Android-ImageView显示选择本地图片或拍照


Android-ImageView显示选择本地图片或拍照

[日期:2012-06-03] 来源:Linux社区  作者:shaojie519 [字体:大 中 小] 

 

http://www.linuxidc.com/Linux/2012-06/61838.htm
 


 

最近写项目,遇到了,在此记录一下。Android-ImageView显示选择本地图片或拍照.
1.import java.io.ByteArrayOutputStream; 
2.import java.io.InputStream; 
3.import android.app.Activity; 
4.import android.app.AlertDialog; 
5.import android.content.ContentResolver; 
6.import android.content.DialogInterface; 
7.import android.content.Intent; 
8.import android.database.Cursor; 
9.import android.graphics.Bitmap; 
10.import android.graphics.BitmapFactory; 
11.import android.net.Uri; 
12.import android.os.Bundle; 
13.import android.provider.MediaStore; 
14.import android.view.View; 
15.import android.view.View.OnClickListener; 
16.import android.widget.ImageView; 
17. 
18.public class PicChooserActivity extends Activity { 
19.    /** Called when the activity is first created. */ 
20.    private ImageView  imageView; 
21.    private OnClickListener imgViewListener; 
22.    private Bitmap myBitmap; 
23.    private byte[] mContent; 
24.    @Override 
25.    public void onCreate(Bundle savedInstanceState) { 
26.        super.onCreate(savedInstanceState); 
27.        setContentView(R.layout.main); 
28.        imageView  =(ImageView) findViewById(R.id.ivPic); 
29.         
30.        imageView.setOnClickListener(new OnClickListener() { 
31.             
32.            @Override 
33.            public void onClick(View v) { 
34.                // TODO Auto-generated method stub  
35.                final CharSequence[] items = { "相册", "拍照" }; 
36.                 
37.                AlertDialog dlg = new AlertDialog.Builder(PicChooserActivity.this).setTitle("选择照片").setItems(items,  
38.                        new DialogInterface.OnClickListener() { 
39.                             
40.                            @Override 
41.                            public void onClick(DialogInterface dialog, int which) { 
42.                                // TODO Auto-generated method stub  
43.                            //这里item是根据选择的方式,   在items数组里面定义了两种方式,拍照的下标为1所以就调用拍照方法    
44.                                if(which==1){ 
45.                                    Intent getImageByCamera  = new Intent("android.media.action.IMAGE_CAPTURE"); 
46.                                    startActivityForResult(getImageByCamera, 1); 
47.                                }else{ 
48.                                    Intent getImage = new Intent(Intent.ACTION_GET_CONTENT); 
49.                                    getImage.addCategory(Intent.CATEGORY_OPENABLE); 
50.                                    getImage.setType("image/jpeg"); 
51.                                    startActivityForResult(getImage, 0); 
52.                                } 
53.                                 
54.                            } 
55.                        }).create(); 
56.                dlg.show(); 
57.            } 
58.        }); 
59.         
60.    } 
61.     
62.     
63.    @Override 
64.    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
65.        // TODO Auto-generated method stub  
66.        super.onActivityResult(requestCode, resultCode, data); 
67.         
68.        ContentResolver contentResolver  =getContentResolver(); 
69.         /**
70.         * 因为两种方式都用到了startActivityForResult方法,这个方法执行完后都会执行onActivityResult方法,
71.         * 所以为了区别到底选择了那个方式获取图片要进行判断,这里的requestCode跟startActivityForResult里面第二个参数对应
72.         */ 
73.         
74.        if(requestCode==0){ 
75.             
76.            //方式一  
77.            /*try {
78.                 //获得图片的uri 
79.                Uri orginalUri = data.getData();
80.                  //将图片内容解析成字节数组 
81.                mContent = readStream(contentResolver.openInputStream(Uri.parse(orginalUri.toString())));
82.                 //将字节数组转换为ImageView可调用的Bitmap对象 
83.                myBitmap  =getPicFromBytes(mContent,null);
84.                  ////把得到的图片绑定在控件上显示
85.                imageView.setImageBitmap(myBitmap);
86.            } catch (Exception e) {
87.                e.printStackTrace();
88.                // TODO: handle exception
89.            }*/ 
90.             
91.            //方式二  
92.            try { 
93.                Uri selectedImage = data.getData(); 
94.                String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
95. 
96.                Cursor cursor = getContentResolver().query(selectedImage, 
97.                        filePathColumn, null, null, null); 
98.                cursor.moveToFirst(); 
99. 
100.                int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
101.                String picturePath = cursor.getString(columnIndex); 
102.                cursor.close(); 
103.                imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 
104.            } catch (Exception e) { 
105.                // TODO: handle exception  
106.                e.printStackTrace(); 
107.            } 
108.             
109.             
110.        }else if(requestCode==1){ 
111.            try { 
112.                Bundle extras = data.getExtras(); 
113.                myBitmap = (Bitmap) extras.get("data"); 
114.                ByteArrayOutputStream baos = new ByteArrayOutputStream();      
115.                myBitmap.compress(Bitmap.CompressFormat.JPEG , 100, baos);      
116.                mContent=baos.toByteArray(); 
117.            } catch (Exception e) { 
118.                e.printStackTrace(); 
119.                // TODO: handle exception  
120.            } 
121.            imageView.setImageBitmap(myBitmap); 
122.        } 
123.         
124.    } 
125.     
126.   public static Bitmap getPicFromBytes(byte[] bytes, BitmapFactory.Options opts) {  
127.        if (bytes != null)  
128.            if (opts != null)  
129.                return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,opts);  
130.            else  
131.                return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);  
132.        return null;  
133.    }  
134.     
135.     
136.     
137. public static byte[] readStream(InputStream in) throws Exception{ 
138.     byte[] buffer  =new byte[1024]; 
139.     int len  =-1; 
140.     ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 
141.      
142.     while((len=in.read(buffer))!=-1){ 
143.         outStream.write(buffer, 0, len); 
144.     } 
145.     byte[] data  =outStream.toByteArray(); 
146.     outStream.close(); 
147.     in.close(); 
148.     return data; 
149. } 
150.     
151.} 

本篇文章来源于 Linux公社网站(www.linuxidc.com)  原文链接:http://www.linuxidc.com/Linux/2012-06/61838.htm

你可能感兴趣的:(Android-ImageView显示选择本地图片或拍照)