SoFunction
Updated on 2025-03-04

Android takes photos to get full-size pictures and compress them

I won’t say much nonsense, I will just post the code to you. The specific code is as follows:

<LinearLayout xmlns:andro
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
  <Button
    android:
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Take Photo" />
  <Button
    android:
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="get Photo" />
  <ImageView
    android:
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_gravity="center_horizontal" />
</LinearLayout>
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
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) {
    (savedInstanceState);
    setContentView(.activity_main);
    takePhoto = (Button) findViewById(.take_photo);
    getPhoto = (Button) findViewById(.get_photo);
    picture = (ImageView) findViewById();
    (this);
    (this);
  }  
  @Override
  public void onClick(View v) {
    switch (()) {
    case .take_photo:
      dispatchTakePictureIntent();
      break;
    default:
      break;
    }
  }
  // Save full-size photos  String mCurrentPhotoPath;
  private void dispatchTakePictureIntent() {
    File appDir = new File((),
        "/etoury/picCache");
    if (!()) {
      ();
    }
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
        .format(new Date());
    String fileName = timeStamp + ".jpg";
    File outputImage = new File(appDir, fileName);
    try {
      if (()) {
        ();
      }
      ();
    } catch (IOException e) {
      ();
    }
    mCurrentPhotoPath = ();
    imgUri = (outputImage);
    // Intent Camera    Intent intent = new Intent(".IMAGE_CAPTURE");
    (MediaStore.EXTRA_OUTPUT, imgUri);
    // If there is a camera    if ((getPackageManager()) != null) {
      startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
    }
  }
  //Decode a Scaled Image  private void setPic() {
    // Get the dimensions of the View
    int targetW = ();
    int targetH = ();
    // Get the dimensions of the bitmap
     bmOptions = new ();
    // Set this value to true, the actual bitmap will not be returned, nor will it allocate memory space to it, thereby avoiding memory overflow.  But allowing us to query the information of the image, including the image size information     = true;
    (mCurrentPhotoPath, bmOptions);
    int photoW = ;
    int photoH = ;
    // Determine how much to scale down the image
    // Find the minimum value    int scaleFactor = (photoW/targetW, photoH/targetH);
    // Decode the image file into a Bitmap sized to fill the View
     = false;
    // Setting the proper inSampleSize allows BitmapFactory to allocate less space to eliminate the error     = scaleFactor;
    // If inPurgeable is set to True, it means that the Bitmap created using BitmapFactory, the memory space used to store Pixel can be recycled when the system memory is insufficient     = true;
    Bitmap bitmap = (mCurrentPhotoPath, bmOptions);
    (bitmap);
  }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
      switch (requestCode) {
      case REQUEST_IMAGE_CAPTURE:
        setPic();
        break;
      default:
        break;
      }
    }
  }
}

The above code is the entire content of Android taking photos and getting full-size pictures and compressing them in this article. I hope you like it.