安卓系统中的摄像头权限被授予后无法使用。

问题描述 投票:0回答:1

我正试图为Android Oreo及以上版本开发一个简单的相机预览应用。在我的 Camera 片段,我有以下方法。

fun hasPermission() = ContextCompat.checkSelfPermission(activity!!, permission.CAMERA) != PackageManager.PERMISSION_GRANTED


override fun onResume() {
    super.onResume()

    if (!hasPermission()) {
        requestPermissions(arrayOf(permission.CAMERA), CAMERA_PERMISSIONS_REQUEST_CODE)
        return
    }

    // access camera
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
    if (requestCode != CAMERA_PERMISSIONS_REQUEST_CODE) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        return
    }

    if (grantResults.filter { it != PackageManager.PERMISSION_GRANTED }.any()) {
        activity?.finish()
        Log.e(null, "Permission denied")
    }

    // access camera
}

然而,当我调试应用程序时,每当我试图访问相机时,它就会崩溃,并有这个堆栈跟踪。

I/Perf: Connecting to perf service.
I/robinson.loupe: [GL_OOM] ClampGrowthLimit 268435456
V/Font: Change font:2
V/Font: Default family:android.graphics.Typeface@3de20b74
E/Perf: Fail to get file list com.richardrobinson.loupe3
    getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
E/Perf: Fail to get file list com.richardrobinson.loupe3
    getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
W/RenderThread: type=1400 audit(0.0:39044): avc: denied { search } for name="gpu" dev="dm-3" ino=8388673 scontext=u:r:untrusted_app:s0:c142,c257,c512,c768 tcontext=u:object_r:vendor_gles_data_file:s0 tclass=dir permissive=0
W/robinson.loupe: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
W/robinson.loupe: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
I/CameraManagerGlobal: Connecting to camera service
E/libc: Access denied finding property "vendor.camera.aux.packagelist"
W/robinson.loupe3: type=1400 audit(0.0:39046): avc: denied { read } for name="u:object_r:persist_camera_prop:s0" dev="tmpfs" ino=16730 scontext=u:r:untrusted_app:s0:c142,c257,c512,c768 tcontext=u:object_r:persist_camera_prop:s0 tclass=file permissive=0
E/libc: Access denied finding property "vendor.camera.aux.packagelist"
W/CameraManagerGlobal: [soar.cts] ignore the status update of camera: 2
E/libc: Access denied finding property "vendor.camera.aux.packagelist"
W/robinson.loupe3: type=1400 audit(0.0:39048): avc: denied { read } for name="u:object_r:persist_camera_prop:s0" dev="tmpfs" ino=16730 scontext=u:r:untrusted_app:s0:c142,c257,c512,c768 tcontext=u:object_r:persist_camera_prop:s0 tclass=file permissive=0
W/CameraManagerGlobal: [soar.cts] ignore the status update of camera: 3
E/libc: Access denied finding property "vendor.camera.aux.packagelist"
W/CameraManagerGlobal: [soar.cts] ignore the status update of camera: 4
E/libc: Access denied finding property "vendor.camera.aux.packagelist"
W/CameraManagerGlobal: [soar.cts] ignore the status update of camera: 5
E/libc: Access denied finding property "vendor.camera.aux.packagelist"
E/libc: Access denied finding property "vendor.camera.aux.packagelist"
I/OPFD_Manager: Follow AOSP false
I/OPFD_Manager: Follow AOSP false
V/ViewRootImpl: The specified message queue synchronization  barrier token has not been posted or has already been removed
D/DecorView: onWindowFocusChangedFromViewRoot hasFocus: true, DecorView@8e1830e[MainActivity]
I/AdrenoGLES: QUALCOMM build                   : bc92c36, I9e73322269
    Build Date                       : 08/28/19
    OpenGL ES Shader Compiler Version: EV031.27.02.00
    Local Branch                     : 
    Remote Branch                    : refs/tags/AU_LINUX_ANDROID_LA.UM.8.1.R1.09.00.00.529.074
    Remote Branch                    : NONE
    Reconstruct Branch               : NOTHING
    Build Config                     : S P 8.0.8 AArch64
I/AdrenoGLES: PFP: 0x016ee187, ME: 0x00000000
D/OpScreenModeManager: setRefreshRate token android.os.BinderProxy@8b5b680 rate 1 mCurrentRate 0
D/OpScreenModeManager: updateScenario fullScreen 1
W/Gralloc3: mapper 3.x is not supported
D/: Successfully load libgui-plugin.so, this=0x7e65d36050
D/OpScreenModeManager: setRefreshRate token android.os.BinderProxy@8b5b680 rate 0 mCurrentRate 1
D/OpScreenModeManager: updateScenario fullScreen 0
E/libc: Access denied finding property "vendor.camera.aux.packagelist"
W/Activity: Can request only one set of permissions at a time
D/OpScreenModeManager: setRefreshRate token android.os.BinderProxy@8b5b680 rate 1 mCurrentRate 0
D/OpScreenModeManager: updateScenario fullScreen 1
D/OnePlusJankManager:  Chor uploadMDM JANK_TYPE_ONCE mViewTitle = com.richardrobinson.loupe3/com.richardrobinson.loupe3.MainActivity--- jank level = 1
W/robinson.loupe3: type=1400 audit(0.0:39063): avc: denied { read } for name="u:object_r:persist_camera_prop:s0" dev="tmpfs" ino=16730 scontext=u:r:untrusted_app:s0:c142,c257,c512,c768 tcontext=u:object_r:persist_camera_prop:s0 tclass=file permissive=0
E/libc: Access denied finding property "vendor.camera.aux.packagelist"
D/DecorView: onWindowFocusChangedFromViewRoot hasFocus: true, DecorView@8e1830e[]
W/robinson.loupe3: type=1400 audit(0.0:39064): avc: denied { read } for name="u:object_r:persist_camera_prop:s0" dev="tmpfs" ino=16730 scontext=u:r:untrusted_app:s0:c142,c257,c512,c768 tcontext=u:object_r:persist_camera_prop:s0 tclass=file permissive=0
E/libc: Access denied finding property "persist.vendor.camera.privapp.list"
E/libc: Access denied finding property "vendor.camera.aux.packagelist"
E/libc: Access denied finding property "vendor.camera.aux.packagelist"
W/Binder:8696_1: type=1400 audit(0.0:39066): avc: denied { read } for name="u:object_r:persist_camera_prop:s0" dev="tmpfs" ino=16730 scontext=u:r:untrusted_app:s0:c142,c257,c512,c768 tcontext=u:object_r:persist_camera_prop:s0 tclass=file permissive=0
E/libc: Access denied finding property "vendor.camera.aux.packagelist"
E/libc: Access denied finding property "persist.vendor.camera.privapp.list"
E/libc: Access denied finding property "vendor.camera.aux.packagelist"
E/libc: Access denied finding property "vendor.camera.aux.packagelist"
E/libc: Access denied finding property "vendor.camera.aux.packagelist"
E/libc: Access denied finding property "vendor.camera.aux.packagelist"
W/System: A resource failed to call release. 
    A resource failed to call release. 
W/Binder:8696_1: type=1400 audit(0.0:39106): avc: denied { read } for name="u:object_r:persist_camera_prop:s0" dev="tmpfs" ino=16730 scontext=u:r:untrusted_app:s0:c142,c257,c512,c768 tcontext=u:object_r:persist_camera_prop:s0 tclass=file permissive=0
E/libc: Access denied finding property "vendor.camera.aux.packagelist"
W/Binder:8696_1: type=1400 audit(0.0:39107): avc: denied { read } for name="u:object_r:persist_camera_prop:s0" dev="tmpfs" ino=16730 scontext=u:r:untrusted_app:s0:c142,c257,c512,c768 tcontext=u:object_r:persist_camera_prop:s0 tclass=file permissive=0
E/libc: Access denied finding property "vendor.camera.aux.packagelist"

还有,这是我的Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.richardrobinson.loupe3">

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>
            <intent-filter>
                <action android:name="android.media.action.IMAGE_CAPTURE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.media.action.VIDEO_CAPTURE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.media.action.STILL_IMAGE_CAMERA" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.media.action.VIDEO_CAMERA" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.glass.TouchEnabledApplication"
            android:value="true" />
    </application>

</manifest>

我遇到的其他StackOverflow的帖子都是通过在Manifest中添加权限检查或权限来解决的 我都做了。

我使用的是最新版本的Windows 10和Android Studio。谢谢

android android-fragments kotlin android-permissions android-camera2
1个回答
-1
投票
 if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) !=PackageManager.PERMISSION_GRANTED) {takePictureButton.setEnabled(false);
 ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
}
@Override
     publicvoidonRequestPermissionsResult(intrequestCode,String[]permissions,int[]grantResults) {
if (requestCode == 0) {
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
            && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
        takePictureButton.setEnabled(true);
    }
}
}


private fun takePicture(View view) {
  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  file = Uri.fromFile(getOutputMediaFile());
  intent.putExtra(MediaStore.EXTRA_OUTPUT, file);

  startActivityForResult(intent, 100);
}



  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == 100) {
     if (resultCode == RESULT_OK) {
        imageView.setImageURI(file);
     }
 }
}


private static File getOutputMediaFile(){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES), "CameraDemo");

if (!mediaStorageDir.exists()){
    if (!mediaStorageDir.mkdirs()){
        return null;
    }
}

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
return new File(mediaStorageDir.getPath() + File.separator +
            "IMG_"+ timeStamp + ".jpg");

}

© www.soinside.com 2019 - 2024. All rights reserved.