Android multimedia calls the camera and selects an image from the local album,
Overview:
The function of this routine is to take photos, automatically compress images, and select images from the local album.
Permission to be loaded:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
Demo:
Public class MainActivity extends Activity implements View. onClickListener {private Button mButtonTakePhoto; private Button mButtonGetPhoto; private ImageView mImageViewPhoto; private File mFile; // The File storing images is public static final int GET_PIC_FROM_CAMERA = 0x123; public static final int GET_PIC_FROM_GALLERY = 0X124; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mButtonTakePhoto = (Button) findViewById (R. id. button_take_photo); mButtonGetPhoto = (Button) findViewById (R. id. button_get_photo); mImageViewPhoto = (ImageView) findViewById (R. id. imageView_camera); // mImageViewPhoto. setImageURI (Uri. fromFile (new File ("/mnt/sdcard/1442309575248.jpg"); mButtonTakePhoto. setOnClickListener (this); mButtonGetPhoto. setOnClickListener (this) ;}@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {super. onActivityResult (requestCode, resultCode, data); switch (requestCode) {case GET_PIC_FROM_CAMERA: if (resultCode = RESULT_ OK) {ImageZip.zip Image (mFile. getAbsolutePath (); // compress the image mImageViewPhoto. setImageURI (Uri. fromFile (mFile);} break; case GET_PIC_FROM_GALLERY: if (resultCode = RESULT_ OK) {getImageFromGallery (data);} break ;}} /*** select and obtain the photo from the local album */private void getImageFromGallery (Intent data) {Uri selectedImage = data. getData (); // use a String array to store all images in the album. String [] filePathColumn = {MediaStore. images. media. DATA}; // use a Cursor object to all content in the album. Cursor cursor = getContentResolver (). query (selectedImage, filePathColumn, null, null); cursor. moveToFirst (); // get the selected image subscript int columnIndex = cursor. getColumnIndex (filePathColumn [0]); // obtain the selected photo path String picturePath = cursor. getString (columnIndex); // disable Cursor to avoid resource usage. close (); ImageZip.zip Image (picturePath); // generally, the Image in the album is too large to be displayed directly. You need to compress the Image. // use an ImageView to display the Image mImageViewPhoto. setImageBitmap (BitmapFactory. decodeFile (picturePath);} @ Override public void onClick (View v) {switch (v. getId () {case R. id. button_take_photo: getPictureFromCamera (); break; case R. id. button_get_photo:/* Intent intent = new Intent (Intent. ACTION_GET_CONTENT); intent. setType (""); startActivityForResult (intent, GET_PIC_FROM_GALLERY); * // left-start parameter: select the action permission, and the URI path of the system's local album is Intent I = new Intent (Intent. ACTION_PICK, android. provider. mediaStore. images. media. EXTERNAL_CONTENT_URI); // send intent to onActivityResult. The requestCode is GET_PIC_FROM_GALLERY startActivityForResult (I, GET_PIC_FROM_GALLERY); break ;}}/*** sends a request to onActivityResult, */private void getPictureFromCamera () {Intent intent = new Intent (); intent. setAction (MediaStore. ACTION_IMAGE_CAPTURE); // determines the image File path obtained by storing the photo. mFile = new File (Environment. getExternalStorageDirectory (), System. currentTimeMillis () + ". jpg "); try {mFile. createNewFile ();} catch (IOException e) {e. printStackTrace ();} // loads the Uri-type file path intent. putExtra (MediaStore. EXTRA_OUTPUT, Uri. fromFile (mFile); // send intent to onActivityResult. The requestCode is GET_PIC_FROM_CAMERA startActivityForResult (intent, GET_PIC_FROM_CAMERA );}}
Compression Image class:
Public class ImageZip {/*** Method for compressing images */public static void zipImage (String savePath) {BitmapFactory. options options = new BitmapFactory. options (); options. inJustDecodeBounds = true; BitmapFactory. decodeFile (savePath, options); options. inSampleSize = computeInitialSampleSize (options, 480,480*960); options. inJustDecodeBounds = false; Bitmap bitmap = BitmapFactory. decodeFile (savePath, options); t Ry {FileOutputStream fos = new FileOutputStream (savePath); bitmap. compress (Bitmap. compressFormat. JPEG, 90, fos); fos. flush (); fos. close ();} catch (IOException e) {e. printStackTrace ();} bitmap. recycle (); bitmap = null; System. gc ();} public static Bitmap getZipImage (String savePath) {BitmapFactory. options options = new BitmapFactory. options (); options. inJustDecodeBounds = true; BitmapFactory.de CodeFile (savePath, options); options. inSampleSize = computeInitialSampleSize (options, 480,480*960); options. inJustDecodeBounds = false; Bitmap bitmap = BitmapFactory. decodeFile (savePath, options); bitmap. recycle (); bitmap = null; System. gc (); return bitmap;} public int computeSampleSize (BitmapFactory. options options, int minSideLength, int maxNumOfPixels) {int initialSize = computeInitialSamp LeSize (options, minSideLength, maxNumOfPixels); int roundedSize; if (initialSize <= 8) {roundedSize = 1; while (roundedSize <initialSize) {roundedSize <= 1 ;}} else {roundedSize = (initialSize + 7)/8*8;} return roundedSize;} public static int computeInitialSampleSize (BitmapFactory. options options, int minSideLength, int maxNumOfPixels) {double w = options. outWidth; double h = options. OutHeight; int lowerBound = (maxNumOfPixels =-1 )? 1: (int) Math. ceil (Math. sqrt (w * h/maxNumOfPixels); int upperBound = (minSideLength =-1 )? 128: (int) Math. min (Math. floor (w/minSideLength), Math. floor (h/minSideLength); if (upperBound <lowerBound) {// return the larger one when there is no overlapping zone. return lowerBound;} if (maxNumOfPixels =-1) & (minSideLength =-1) {return 1;} else if (minSideLength =-1) {return lowerBound;} else {return upperBound ;}}}
Layout:
<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"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button_take_photo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Take Photo"/> <Button android:id="@+id/button_get_photo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Get From Gallery"/> </LinearLayout> <ImageView android:id="@+id/imageView_camera" android:layout_width="match_parent" android:layout_height="match_parent"/></LinearLayout>
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.