Android 6.0 calls perfect solution for Camera Atlas crash _android

Source: Internet
Author: User

Recently, the customer update system found that the previous project in the call to the camera, Flash returned, very strange, later found that Android 6.0 after the need for program authorization camera permissions, the default will give a hint, let the user authorization, personal feeling this feature is very good, probably as follows:

Import Android V4, V7 pack!

Android Studio Import is very simple, right key items found after dependency OK.

Inherit appcompatactivity

public class Mainactivity extends appcompatactivity

Introducing the required class libraries

Import Android.support.design.widget.Snackbar;
Import Android.support.v4.app.ActivityCompat;
Import android.support.v7.app.AppCompatActivity;

Check camera permissions and request permissions

Begin_include (Camera_permission_request) if (Activitycompat.shouldshowrequestpermissionrationale) (This,  Manifest.permission.CAMERA)) {//provide an additional rationale to the user if the permission is not granted//and the
User would benefit from additional to the "use of" of the permission.
For example if the user has previously denied the permission.
LOG.I (TAG, "displaying camera permission rationale to provide the context."); Snackbar.make (Mlayout, R.string.permission_camera_rationale, Snackbar.length_indefinite). SetAction (R.string.ok, New View.onclicklistener () {@Override public void OnClick (view view) {Activitycompat.requestpermissions (
Mainactivity.this, New String[]{manifest.permission.camera}, Request_camera);
). Show (); else {//Camera permission has not been granted yet.
Request it directly.
Activitycompat.requestpermissions (this, new String[]{manifest.permission.camera}, Request_camera); }//End_include (Camera_permission_request)

Receive callback method after authorization:

/**
* Callback received when a permissions request has been.
*
/@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.
LOG.I (TAG, "Received response for Camera permission request.");
Check if the only required permission has been granted
} 
}

Hybrid Application Development Solutions

For mixed-application development, there are two types of solutions:

Change existing plug-ins, add permission access code (there may be some trouble)

Invoke Permissions Plug-in:

Installation:

Cordova Plugin Add cordova-plugin-android-permissions@0.10.0

Included permissions

Example 
permissions. Access_coarse_location
permissions. CAMERA
permissions. Get_accounts
permissions. Read_contacts
permissions. Read_calendar ...

Sample code

var permissions = cordova.plugins.permissions;
Permissions.haspermission (Permissions. CAMERA, checkpermissioncallback, null);
function Checkpermissioncallback (status) {
if (!status.haspermission) {
var errorcallback = function () {
Console.warn (' Camera permission isn't turned on ');
Permissions.requestpermission (
permissions. CAMERA,
function (status) {
if (!status.haspermission) Errorcallback ();
},
errorcallback);
}

The above is a small set to introduce the Android 6.0 call Camera Atlas collapse of the perfect solution, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.