1 package com.testCarema.android; 2 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.graphics.Bitmap; 9 import android.graphics.BitmapFactory; 10 import android.net.Uri; 11 import android.os.Bundle; 12 import android.view.View; 13 import android.view.View.OnClickListener; 14 import android.widget.ImageView; 15 16 import java.io.ByteArrayOutputStream; 17 import java.io.InputStream; 18 19 public class testCarema extends Activity { 20 21 private ImageView imageView; 22 private OnClickListener imgViewListener; 23 private Bitmap myBitmap; 24 private byte[] mContent; 25 26 @Override 27 public void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.main); 30 imageView = (ImageView) findViewById(R.id.imageView); 31 imgViewListener = new OnClickListener() { 32 public void onClick(View v) { 33 final CharSequence[] items = {"相册", "拍照"}; 34 AlertDialog dlg = new AlertDialog.Builder(testCarema.this) 35 .setTitle("选择图片") 36 .setItems(items, new DialogInterface.OnClickListener() { 37 public void onClick(DialogInterface dialog, int item) { 38 // 这里item是根据选择的方式, 39 // 在items数组里面定义了两种方式,拍照的下标为1所以就调用拍照方法 40 if (item == 1) { 41 Intent getImageByCamera = new Intent( 42 "android.media.action.IMAGE_CAPTURE"); 43 startActivityForResult(getImageByCamera, 1); 44 } else { 45 Intent getImage = new Intent( 46 Intent.ACTION_GET_CONTENT); 47 getImage.addCategory(Intent.CATEGORY_OPENABLE); 48 getImage.setType("image/jpeg"); 49 startActivityForResult(getImage, 0); 50 } 51 } 52 }).create(); 53 dlg.show(); 54 } 55 }; 56 // 给imageView控件绑定点点击监听器 57 imageView.setOnClickListener(imgViewListener); 58 59 } 60 61 @Override 62 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 63 // TODO Auto-generated method stub 64 super.onActivityResult(requestCode, resultCode, data); 65 66 ContentResolver resolver = getContentResolver(); 67 /** 68 * 因为两种方式都用到了startActivityForResult方法, 这个方法执行完后都会执行onActivityResult方法, 69 * 所以为了区别到底选择了那个方式获取图片要进行判断, 70 * 这里的requestCode跟startActivityForResult里面第二个参数对应 71 */ 72 if (requestCode == 0) { 73 try { 74 // 获得图片的uri 75 Uri originalUri = data.getData(); 76 // 将图片内容解析成字节数组 77 mContent = readStream(resolver.openInputStream(Uri 78 .parse(originalUri.toString()))); 79 // 将字节数组转换为ImageView可调用的Bitmap对象 80 myBitmap = getPicFromBytes(mContent, null); 81 // //把得到的图片绑定在控件上显示 82 imageView.setImageBitmap(myBitmap); 83 } catch (Exception e) { 84 System.out.println(e.getMessage()); 85 } 86 87 } else if (requestCode == 1) { 88 try { 89 super.onActivityResult(requestCode, resultCode, data); 90 Bundle extras = data.getExtras(); 91 myBitmap = (Bitmap) extras.get("data"); 92 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 93 myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 94 mContent = baos.toByteArray(); 95 } catch (Exception e) { 96 // TODO Auto-generated catch block 97 e.printStackTrace(); 98 } 99 // 把得到的图片绑定在控件上显示100 imageView.setImageBitmap(myBitmap);101 }102 }103 104 public static Bitmap getPicFromBytes(byte[] bytes,105 BitmapFactory.Options opts) {106 if (bytes != null)107 if (opts != null)108 return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,109 opts);110 else111 return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);112 return null;113 }114 115 public static byte[] readStream(InputStream inStream) throws Exception {116 byte[] buffer = new byte[1024];117 int len = -1;118 ByteArrayOutputStream outStream = new ByteArrayOutputStream();119 while ((len = inStream.read(buffer)) != -1) {120 outStream.write(buffer, 0, len);121 }122 byte[] data = outStream.toByteArray();123 outStream.close();124 inStream.close();125 return data;126 127 }128 129 }