Andriod open the camera and album, andriod camera album
Package com. example. yanlei. picture; import android. support. v7.app. appCompatActivity; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import java. io. file; import java. io. IOException; import android. app. activity; import android. content. intent; import android. database. cursor; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. matrix; imp Ort android. media. exifInterface; import android.net. uri; import android. OS. bundle; import android. OS. environment; import android. provider. mediaStore; import android. util. log; import android. view. view; import android. widget. button; import android. widget. imageView; import android. widget. toast; public class MainActivity extends AppCompatActivity {private static final String tag = "MainActivity"; private stati C final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100; private static final int PICK_IMAGE_ACTIVITY_REQUEST_CODE = 200; private ImageView imageView; @ Override protected void onCreate (Bundle savedInstanceState. onCreate (savedInstanceState); setContentView (R. layout. activity_main); imageView = (ImageView) this. findViewById (R. id. image_view); Button button = (Button) this. findViewById (R. id. open _ Camera); button. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {takePicture () ;}}); Button pickImageBtn = (Button) this. findViewById (R. id. pick_image); pickImageBtn. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {openAlbum () ;}}) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the Menu; this Dds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. menu_main, menu); return true;} @ Override public boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest. xml. int id = item. getItemId (); // noinspection S ImplifiableIfStatement if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item);} private static String picFileFullName; // photograph public void takePicture () {String state = Environment. getExternalStorageState (); if (state. equals (Environment. MEDIA_MOUNTED) {Intent intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE); File outDir = Environment. getExternalStoragePublicD Irectory (Environment. DIRECTORY_PICTURES); if (! OutDir. exists () {outDir. mkdirs ();} File outFile = new File (outDir, System. currentTimeMillis () + ". jpg "); picFileFullName = outFile. getAbsolutePath (); intent. putExtra (MediaStore. EXTRA_OUTPUT, Uri. fromFile (outFile); intent. putExtra (MediaStore. EXTRA_VIDEO_QUALITY, 1); startActivityForResult (intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);} else {Log. e (tag, "Please make sure the SD card has been inserted") ;}// open the local album public void ope NAlbum () {Intent intent = new Intent (); intent. setType ("image/*"); intent. setAction (Intent. ACTION_GET_CONTENT); this. startActivityForResult (intent, PICK_IMAGE_ACTIVITY_REQUEST_CODE);} @ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {super. onActivityResult (requestCode, resultCode, data); if (requestCode = CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {if (resultCo De = RESULT_ OK) {Log. e (tag, "image retrieved successfully, path =" + picFileFullName); toast ("image retrieved successfully, path =" + picFileFullName); setImageView (picFileFullName );} else if (resultCode = RESULT_CANCELED) {// The user canceled image capture} else {// image capture failed, prompting the user Log. e (tag, "photo failed") ;}} else if (requestCode = PICK_IMAGE_ACTIVITY_REQUEST_CODE) {if (resultCode = RESULT_ OK) {Uri uri = data. getData (); if (uri! = Null) {String realPath = getRealPathFromURI (uri); Log. e (tag, "image retrieved successfully, path =" + realPath); toast ("image retrieved successfully, path =" + realPath); setImageView (realPath);} else {Log. e (tag, "failed to get image from album") ;}}} private void setImageView (String realPath) {Bitmap bmp = BitmapFactory. decodeFile (realPath); int degree = readPictureDegree (realPath); if (degree <= 0) {imageView. setImageBitmap (bmp);} else {Log. e (tag, "rotate:" + degree); // create an image using the matrix object Matrix = new matrix (); // rotate the image action Matrix. postRotate (degree); // create a new image. Bitmap resizedBitmap = Bitmap. createBitmap (bmp, 0, 0, bmp. getWidth (), bmp. getHeight (), matrix, true); imageView. setImageBitmap (resizedBitmap );}} /*** This method is used to get real path of file from uri <br/> * http://stackoverflow.com/questions/11591825/how-to-get-image-path-just-captured-from-camera ** @ param contentUri * @ return String */public String getRealPathFromURI (Uri contentUri) {try {String [] proj = {MediaStore. images. media. DATA}; // Do not call Cursor. close () on a cursor obtained using this method, // because the activity will do that for you at the appropriate time Cursor cursor = this. managedQuery (contentUri, proj, null); int column_index = cursor. getColumnIndexOrThrow (MediaStore. images. media. DATA); cursor. moveToFirst (); return cursor. getString (column_index);} catch (Exception e) {return contentUri. getPath ();}} /*** read the rotation angle in the photo exif information <br/> * http://www.eoeandroid.com/thread-196978-1-1.html ** @ param path photo path * @ return angle */public static int readPictureDegree (String path) {int degree = 0; try {ExifInterface exifInterface = new ExifInterface (path); int orientation = exifInterface. getAttributeInt (ExifInterface. TAG_ORIENTATION, ExifInterface. ORIENTATION_NORMAL); switch (orientation) {case ExifInterface. ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface. ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface. ORIENTATION_ROTATE_270: degree = 270; break ;}} catch (IOException e) {e. printStackTrace ();} return degree;} public void toast (String msg) {Toast. makeText (this, msg, Toast. LENGTH_SHORT ). show ();}}
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <Button android: id = "@ + id/open_camera" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Open the camera"/> <Button android: id = "@ + id/pick_image" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Open album"/> <ImageView android: id = "@ + id/image_view" android: layout_width = "300dp" android: layout_height = "300dp" android: scaleType = "fitCenter"/> </LinearLayout>