Take a photo (2) Get a full-size image, compress it, and take the photo size
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/take_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Take Photo" /> <Button android:id="@+id/get_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="get Photo" /> <ImageView android:id="@+id/picture" android:layout_width="300dp" android:layout_height="300dp" android:layout_gravity="center_horizontal" /></LinearLayout>
Package com. example. choosepictest; import java. io. file; import java. io. IOException; import java. text. simpleDateFormat; import java. util. date; import android. app. activity; import android. content. intent; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android.net. uri; import android. OS. bundle; import android. OS. environment; import android. provider. mediaStore; import android. view. view; Import android. view. view. onClickListener; import android. widget. button; import android. widget. imageView; public class MainActivity extends Activity implements OnClickListener {static final int REQUEST_IMAGE_CAPTURE = 1; private Button takePhoto; private Button getPhoto; private ImageView picture; private Uri imgUri; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (saved InstanceState); setContentView (R. layout. activity_main); takePhoto = (Button) findViewById (R. id. take_photo); getPhoto = (Button) findViewById (R. id. get_photo); picture = (ImageView) findViewById (R. id. picture); takePhoto. setOnClickListener (this); getPhoto. setOnClickListener (this) ;}@ Override public void onClick (View v) {switch (v. getId () {case R. id. take_photo: dispatchTakePictureIntent (); break; Default: break ;}// Save the full-size photo String mCurrentPhotoPath; private void dispatchTakePictureIntent () {File appDir = new File (Environment. getExternalStorageDirectory (), "/etoury/picCache"); if (! AppDir. exists () {appDir. mkdirs ();} String timeStamp = new SimpleDateFormat ("yyyyMMdd_HHmmss "). format (new Date (); String fileName = timeStamp + ". jpg "; File outputImage = new File (appDir, fileName); try {if (outputImage. exists () {outputImage. delete ();} outputImage. createNewFile ();} catch (IOException e) {e. printStackTrace ();} mCurrentPhotoPath = outputImage. getAbsolutePath (); imgUri = Ur I. fromFile (outputImage); // Intent camera intent Intent = new Intent ("android. media. action. IMAGE_CAPTURE "); intent. putExtra (MediaStore. EXTRA_OUTPUT, imgUri); // if there is a camera if (intent. resolveActivity (getPackageManager ())! = Null) {startActivityForResult (intent, REQUEST_IMAGE_CAPTURE) ;}// Decode a Scaled Image private void setPic () {// Get the dimensions of the View int targetW = picture. getWidth (); int targetH = picture. getHeight (); // Get the dimensions of the bitmap BitmapFactory. options bmOptions = new BitmapFactory. options (); // if this value is set to true, the actual bitmap will not be returned, nor the memory space will be allocated to it to avoid memory overflow. However, we can query the image information, which includes the image size information bmOptions. inJustDecodeBounds = true; BitmapFactory. decodeFile (mCurrentPhotoPath, bmOptions); int photoW = bmOptions. outWidth; int photoH = bmOptions. outHeight; // Determine how much to scale down the image // Math. min calculates the minimum value int scaleFactor = Math. min (photoW/targetW, photoH/targetH); // Decode the image file into a Bitmap sized to fill the View bmOptions. inJustDecodeBounds = false; // setting the appropriate inSampleSize can make BitmapFactory allocate less space to eliminate this error bmOptions. inSampleSize = scaleFactor; // If inPurgeable is set to True, the Bitmap created by BitmapFactory is used to store Pixel's memory space. When the system memory is insufficient, bmOptions can be recycled. inPurgeable = true; Bitmap bitmap = BitmapFactory. decodeFile (mCurrentPhotoPath, bmOptions); picture. setImageBitmap (bitmap);} @ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {if (resultCode = RESULT_ OK) {switch (requestCode) {case REQUEST_IMAGE_CAPTURE: galleryAddPic (); setPic (); break; default: break ;}}}
// Send a notification to add a photo to the album
Private void galleryAddPic (){
Intent mediaScanIntent = new Intent (Intent. ACTION_MEDIA_SCANNER_SCAN_FILE );
MediaScanIntent. setData (imgUri );
This. sendBroadcast (mediaScanIntent );
}}