Without further ado, look directly at the effect:

video Taking pictures File selection (get the first frame)

Note:

  • Video/Photo is an open source framework used with a few tweaks. I can’t find the original address.

Preparation before work

This article involves using the camera, saving files and other operations, so you need to apply for permission first

Permission application is to use Guo Lin’s open source library permissionX

Paste the complete code directly:

// Request permission
    private fun initPermission(a) {
        PermissionX.init(this)
            .permissions(
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.CAMERA
            )
            //onExplainRequestReason is executed when the user denies permission
            .onExplainRequestReason { deniedList ->
                showRequestReasonDialog(deniedList, "The permissions to be reapplied are the permissions that the program must depend on."."I have understood."."Cancel")}//onForwardToSettings listens for permissions permanently denied by the user
            .onForwardToSettings {
                showForwardToSettingsDialog(it, "You need to go to application Settings and manually enable permissions."."I have understood."."Cancel")}/** * Parameter 1: indicates whether all permissions pass. Parameter 2: records all authorized permissions. Parameter 3: indicates all denied permissions. * /
            .request { allGranted, _, _ ->
                run {
                    if (allGranted) {
                        // Permission passed
                    } else {
                        showToast("Permission failed")}}}}Copy the code

Make sure to apply in androidmanifest.xml

Save files above Android10(targetSdkVersion >= 29) need to add another:

Take photos/video

Go to the CameraActivity page, the CameraActivity page is library, download the complete Demo and import into it

  • MaxTime Indicates the maximum shooting time (unit :s). The default value is 15 seconds
  • MinTime indicates the minimum shooting time (unit :s). The default value is 1s
        val intent = Intent(this, CameraActivity::class.java)

        // Set the maximum shooting value in seconds
        intent.putExtra("maxTime".20)
        // Set the shooting minimum in seconds
        intent.putExtra("minTime".1)
        startActivityForResult(intent, 100)
Copy the code

Receive the callback from the CameraActivity page

  • 101 is the successful callback image path
  • 103: no No permission
  • 104 for video file callbacks (used below)
 override fun onActivityResult(requestCode: Int, resultCode: Int.data: Intent?). {
        super.onActivityResult(requestCode, resultCode, data)
        Log.i("szjCode"."$resultCode\t$requestCode")

        if (resultCode == 101) {
            val path = data? .getStringExtra("path")
            // Set the callback to the first frame photo
            photo.setImageBitmap(BitmapFactory.decodeFile(path))
        }

        if (resultCode == 103) {
            Toast.makeText(this."Please check camera permissions ~", Toast.LENGTH_SHORT).show()
        }

        // Call back on file
        if (requestCode == 104) {
            // Get the full path of the image
            val path = ContentUriUtil.getPath(this.data? .data)
            Log.i("filepath".= ""$path")

            showToast("The full path of the video you selected is:$path")
            // Get the first frame of the image and assign it
            getVideoThumbnail(path)
        }
    }
Copy the code

Let’s review the renderings:

Record the screen Taking pictures

Gets the first frame of the video file

To get the first frame of the video file, you need to get the complete video path first

Enter the video file selection page (select only videos)

        val intent = Intent()
        Type set to image */
        //intent.setType("image/*");
        // intent.setType("audio/*"); // Select audio
        intent.type = "video/*" // Select video (mp4 3GP is the video format supported by Android)

        // intent.setType("video/*; image/*"); // Select both video and image

        /* Use intent.action_get_content for Action */
        intent.action = Intent.ACTION_GET_CONTENT
        /* Get the photo and return to this screen */
        startActivityForResult(intent, 104)
Copy the code

Video file callback

  • ContentUriUtil.getPath(this, data? .data) through the URL to obtain the selection of video path, this class is I find on the Internet, pro test useful
  • GetVideoThumbnail () gets the first frame of the video file
 override fun onActivityResult(requestCode: Int, resultCode: Int.data: Intent?). {
        super.onActivityResult(requestCode, resultCode, data)
        Log.i("szjCode"."$resultCode\t$requestCode")...// Call back on file
        if (requestCode == 104) {
            // Get the full path of the image
            val path = ContentUriUtil.getPath(this.data? .data)
            Log.i("filepath".= ""$path")

            showToast("The full path of the video you selected is:$path")
            // Get the first frame of the image and assign it
            getVideoThumbnail(path)
        }
    }
Copy the code

Get the first frame picture of the video file:

// Get the first frame of the video file
    private fun getVideoThumbnail(url: String?). {
        val bitmap: Bitmap?
        //MediaMetadataRetriever is a class defined in Android that provides unity
        // interface for retrieving frames and metadata from input media files;
        val retriever = MediaMetadataRetriever()
        // () Get thumbnails according to the file path
        retriever.setDataSource(url);

        // Get the first frame
        bitmap = retriever.frameAtTime

        // Set the image
        photo.setImageBitmap(bitmap)
        / / release
        retriever.release()
    }
Copy the code

A final review of the results:

Extracurricular knowledge

Get the control Id quickly

 // Add the kotlin application extension to your plugins
    id 'kotlin-android-extensions'
Copy the code

Use:

Need to add a guide package: (commond + Enter)

This plugin has been out for a long time, I am the national army in 49 years, just introduced kt project, so I just know. Very good use of the recommendation and I do not know the same students ~

The complete code

Original is not easy, your praise is the biggest support for me!