SoFunction
Updated on 2025-03-11

The perfect solution for crashing camera album in Android 6.0

Recently, the customer updated the system and found that the previous project crashed when calling the camera. It was very strange. Later, after checking it, it was found that after Android 6.0, the program needs to authorize the camera permissions. The default prompt will be given to let the user authorize it. I personally feel that this feature is very good, roughly as follows:

Import Android V4, V7 packages!

Android Studio import is very simple. Right-clicking the project and finding dependency is OK.

Inherit AppCompatActivity

public class MainActivity extends AppCompatActivity

Introduce the required class library

import ;
import .;
import .;

Check camera permissions and request permissions

// BEGIN_INCLUDE(camera_permission_request)
if ((this,
)) {
// Provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// For example if the user has previously denied the permission.
(TAG,
"Displaying camera permission rationale to provide additional context.");
(mLayout, .permission_camera_rationale,
Snackbar.LENGTH_INDEFINITE)
.setAction(, new () {
@Override
public void onClick(View view) {
(,
new String[]{},
REQUEST_CAMERA);
}
})
.show();
} else {
// Camera permission has not been granted yet. Request it directly.
(this, new String[]{},
REQUEST_CAMERA);
}
// END_INCLUDE(camera_permission_request)

Method of receiving callback after authorization:

/**
* Callback received when a permissions request has been completed.
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA) {
// BEGIN_INCLUDE(permission_result)
// Received permission result for camera permission.
(TAG, "Received response for Camera permission request.");
// Check if the only required permission has been granted
} 
}

Hybrid application development solutions

For hybrid application development, there are two solutions:

Change existing plugins, add permission access code (maybe some troublesome)

Call permission plugin:

Install:

cordova plugin add [email protected]

Permissions included

// Example 
permissions.ACCESS_COARSE_LOCATION

permissions.GET_ACCOUNTS
permissions.READ_CONTACTS
permissions.READ_CALENDAR
...

Sample code

var permissions = ;
(, checkPermissionCallback, null);
function checkPermissionCallback(status) {
if(!) {
var errorCallback = function() {
('Camera permission is not turned on');
}
(
,
function(status) {
if(!) errorCallback();
},
errorCallback);
}
}

The above is the perfect solution for the crash of Android 6.0 calling camera albums introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!