KongzueTakePhoto

Kongzue APP Photo & Album selection tool

Github:https://github.com/kongzue/TakePhoto

Introduce TakePhoto into your project

Introduction method:

Maven:

<dependency> <groupId>com.kongzue. Takephoto </artifactId> takephoto</artifactId> <version>2.0.0</version> <type>pom</type>
</dependency>
Copy the code

Gradle:

implementation 'com. Kongzue. Takephoto: takephoto: 2.0.0'
Copy the code

instructions

  1. Android 6.0 and above will automatically apply for permissions, but you still need to pre-declare camera permissions and storage read and write permissions in your project. The permission application process takes place automatically. For requesting permissions, you must inherit activities from AppCompatActivity before invoking this tool. This tool can be used singleton way. Activity extends AppCompatActivity must be passed to getInstance().
  2. This tool only provides the default single photo shooting and single photo selection in the album function.
  3. This tool integration image compression by default CompressHelper framework (https://github.com/nanchen2251/CompressHelper) thank @ nanchen2251 contribution to open source.
  4. This tool has dealt with the problem that in Android 7.0 or above, the system prevents apps from transferring URIs to each other, which may result in the failure to call the camera to take photos and store them in the specified directory. Do not worry about this problem, rest assured to use.
  5. The comparison table of parameters you need to provide for this tool is as follows:

Image compression related:

attribute meaning instructions
DEFAULT_QUALITY Image quality Optional, default 80 (%)
DEFAULT_MAX_WIDTH Maximum picture width optional
DEFAULT_MAX_HEIGHT Maximum height of picture optional
DEFAULT_PIC_TYPE Image output type Optional (Bitmap.CompressFormat type)

Function related:

methods meaning Whether must
doOpenCamera() Call the camera to take a picture optional
doOpenGallery() Call album to select the photo optional
onActivityResult( requestCode, resultCode, data) Override the onActivityResult method in your Activity and pass the parameters into the tool’s method Must be
setReturnPhoto(ReturnPhoto) Callback listeners optional

Required permissions:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Copy the code

To prepare

  1. Modify androidmanifest.xml to add the above permissions.

Then initialize TakePhoto:

TakePhotoUtil. GetInstance (your Activity). SetReturnPhoto (new TakePhotoUtil.ReturnPhoto() { @Override public void onGetPhoto(String path, Bitmap bitmap) { } @Override public void onError(Exception e) { e.printStackTrace(); }});Copy the code

Note that the permission request is triggered the first time you call the getInstance() method.

In this callback method, path is the returned file path and bitmap is the processed bitmap data. If an error occurs, it will be returned in onError.

  1. Override the onActivityResult method in your Activity and pass its data to TakePhotoUtil:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    TakePhotoUtil.getInstance(MainActivity.this).onActivityResult(requestCode, resultCode, data);
}
Copy the code
  1. Call the corresponding method to use the camera and album:
/ / use camera TakePhotoUtil. GetInstance (MainActivity. This). DoOpenCamera (); / / use photo album choice TakePhotoUtil getInstance (MainActivity. This). DoOpenGallery ();Copy the code

other

Adjust image compression options:

// Initialize takePhotoutil. DEFAULT_QUALITY = 90; // Compression frame: image quality takephotoutil. DEFAULT_MAX_WIDTH = 1080; // Compression frame: the maximum width of the image takephotoutil. DEFAULT_MAX_HEIGHT = 1080; / / compression framework: picture TakePhotoUtil maximum height. The DEFAULT_PIC_TYPE = Bitmap.Com pressFormat. JPEG. // Compression frame: default compression formatCopy the code

Update log:

V2.0.0:

  • Replaced the picture compression frame;
  • Android Support library updated to 27.1.0.

V1.0:

  • A new release

PS:

Laziness is the greatest source of productivity… XD