Package com. amani. main; Import java. io. ByteArrayOutputStream; Import java. io. InputStream; Import android. app. Activity; Import android. app. AlertDialog; Import android. content. ContentResolver; Import android. content. DialogInterface; Import android. content. Intent; Import android. graphics. Bitmap; Import android. graphics. BitmapFactory; Import android.net. Uri; Import android. OS. Bundle; Import android. view. View; Import android. view. View. OnClickListener; Import android. widget. ImageView; Public class chooser extends Activity { Private ImageView imageView; Private OnClickListener imgViewListener; Bitmap myBitmap; Private byte [] mContent;
@ Override Public void onCreate (Bundle savedInstanceState ){ Super. onCreate (savedInstanceState ); SetContentView (R. layout. main ); ImageView = (ImageView) findViewById (R. id. ivPic );
ImgViewListener = new OnClickListener (){ Public void onClick (View v ){ Final CharSequence [] items = {"album", "photo "}; AlertDialog dlg = new AlertDialog. Builder (chooser. this). setTitle ("select image"). setItems (items, New DialogInterface. OnClickListener (){ Public void onClick (DialogInterface dialog, int item ){ // Here, item is based on the selected method. Two methods are defined in the items array. The subscript of the image is 1, so the photo method is called. If (item = 1 ){ Intent getImageByCamera = new Intent ("android. media. action. IMAGE_CAPTURE "); StartActivityForResult (getImageByCamera, 1 ); } Else { Intent getImage = new Intent (Intent. ACTION_GET_CONTENT ); GetImage. addCategory (Intent. CATEGORY_OPENABLE ); GetImage. setType ("image/jpeg "); StartActivityForResult (getImage, 0 ); } } }). Create (); Dlg. show (); } }; // Bind the click listener to the imageView Control ImageView. setOnClickListener (imgViewListener );
}
@ Override Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
ContentResolver resolver = getContentResolver (); /** * Because the startActivityForResult method is used in both methods, the onActivityResult method is executed after the method is executed, * In order to determine whether to choose the method to obtain the image, the requestCode here corresponds to the second parameter in startActivityForResult. */ If (requestCode = 0 ){ Try { // Obtain the image uri Uri originalUri = data. getData (); // Parse the image content into a byte array MContent = readStream (resolver. openInputStream (Uri. parse (originalUri. toString ()))); // Convert the byte array to a Bitmap object that can be called by ImageView. MyBitmap = getPicFromBytes (mContent, null ); //// Bind the obtained image to the control for display ImageView. setImageBitmap (myBitmap ); } Catch (Exception e ){ System. out. println (e. getMessage ()); } } Else if (requestCode = 1 ){ Try { Super. onActivityResult (requestCode, resultCode, data ); Bundle extras = data. getExtras (); MyBitmap = (Bitmap) extras. get ("data "); ByteArrayOutputStream baos = new ByteArrayOutputStream (); MyBitmap. compress (Bitmap. CompressFormat. JPEG, 100, baos ); MContent = baos. toByteArray (); } Catch (Exception e ){ // TODO Auto-generated catch block E. printStackTrace (); } // Bind the obtained image to the control for display ImageView. setImageBitmap (myBitmap ); } } Public static Bitmap getPicFromBytes (byte [] bytes, BitmapFactory. Options opts ){ If (bytes! = Null) If (opts! = Null) Return BitmapFactory. decodeByteArray (bytes, 0, bytes. length, opts ); Else Return BitmapFactory. decodeByteArray (bytes, 0, bytes. length ); Return null; }
Public static byte [] readStream (InputStream inStream) throws Exception { Byte [] buffer = new byte [1024]; Int len =-1; ByteArrayOutputStream outStream = new ByteArrayOutputStream (); While (len = inStream. read (buffer ))! =-1 ){ OutStream. write (buffer, 0, len ); } Byte [] data = outStream. toByteArray (); OutStream. close (); InStream. close (); Return data; } } |