Hello world, today i have problem when i develop android app. For first time i see, i so shock about it. Lol. Actually apps its really done, for Jelly Bean OS, but when i try to new Marshmallow OS i got some error. Yap this is is ' Fail to connect to camera service '. And i already set permission on manifest file, like this one.
<uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.CAMERA" ></uses-permission>
So whats happen? After i did try to find, i got solution about this case. So marshmallow have different permission to get it. You can read on this article Android Developer . On the marshmallow os we need to set additional permission on java code too.
Before I show to create additional permission on java code, i will make you sure about this case. On my case i got problem when i show camera in my apps, and the error is 'Fail to connect to camera service' . This problem cause i not yet to set additional permission on my java code. This is happen just on marshmallow OS, so bellow marshmallow os we don't need to set this. Note that.
This is my java code to set additional permission.
@Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case 111: { for (int i = 0; i < permissions.length; i++) { if (grantResults[i] == PackageManager.PERMISSION_GRANTED) { System.out.println("Permissions --> " + "Permission Granted: " + permissions[i]); } else if (grantResults[i] == PackageManager.PERMISSION_DENIED) { System.out.println("Permissions --> " + "Permission Denied: " + permissions[i]); } } } } @Override public void onStart() { super.onStart(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { int hasCameraPermission = getActivity().checkSelfPermission(Manifest.permission.CAMERA); int hasWritePermission = getActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE); List<String> permissions = new ArrayList<String>(); if (hasCameraPermission != PackageManager.PERMISSION_GRANTED) { permissions.add(Manifest.permission.CAMERA); } if (!permissions.isEmpty()) { requestPermissions(permissions.toArray(new String[permissions.size()]), 111); } } }
So on this code i have conclusion like this :
1. On function onStart() it will show pop up to set our application for first time activity run. If users agree about our permission, application will got permission. Ass you can see, permission will be save on variable list permission permissions.add(Manifest.permission.CAMERA); .
2. On the function onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) it will show message we get permission on apps or not. If our application get permission, it will move on this condition :
(grantResults[i] == PackageManager.PERMISSION_GRANTED)
and if else will be move on this condition
(grantResults[i] == PackageManager.PERMISSION_DENIED)
Just it, i hope this is can help you, if you have same problem like me. You can try this way, i hope we can learn together for develop beautiful android apps. And last i wanna say, don't forget to comment bellow if you have confuse about my code. Thanks.
This comment has been removed by the author.
ReplyDelete