Android 调用摄像头应用程序(权限拒绝:启动意图)

Android 调用摄像头应用程序(权限拒绝:启动意图),android,android-camera,android-manifest,android-permissions,android-camera-intent,Android,Android Camera,Android Manifest,Android Permissions,Android Camera Intent,我的片段正在尝试启动摄像头活动,但似乎有一些摄像头权限问题 在调试过程中,它只运行到代码行下方 startActivityForResult(callCameraApplicationIntent, CAMERA_PIC_REQUEST); 下面是logcat结果 java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cm

我的片段正在尝试启动摄像头活动,但似乎有一些摄像头权限问题

在调试过程中,它只运行到代码行下方

startActivityForResult(callCameraApplicationIntent, CAMERA_PIC_REQUEST);

下面是logcat结果

java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=android/com.android.internal.app.ResolverActivity clip={text/uri-list U:content://com.example.tc.provider/external_files/Pictures/photo_saving_app/IMAGE_20191205_070208.jpg} (has extras) } from ProcessRecord{8920903 18662:com.example.tc/u0a343} (pid=18662, uid=10343) with revoked permission android.permission.CAMERA
        at android.os.Parcel.readException(Parcel.java:2016)
        at android.os.Parcel.readException(Parcel.java:1962)
        at android.app.IActivityManager$Stub$Proxy.startActivity(IActivityManager.java:4452)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1617)
        at android.app.Activity.startActivityForResult(Activity.java:4551)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
下面是代码中似乎需要的部分


        if (Build.VERSION.SDK_INT > 21) { //use this if Lollipop_Mr1 (API 22) or above
            Intent callCameraApplicationIntent = new Intent();
            callCameraApplicationIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

            // We give some instruction to the intent to save the image
            File photoFile = null;

            try {
                // If the createImageFile will be successful, the photo file will have the address of the file
                photoFile = createImageFile();
                // Here we call the function that will try to catch the exception made by the throw function
            } catch (IOException e) {
                Logger.getAnonymousLogger().info("Exception error in generating the file");
                e.printStackTrace();
            }
            // Here we add an extra file to the intent to put the address on to. For this purpose we use the FileProvider, declared in the AndroidManifest.
            Uri outputUri = FileProvider.getUriForFile(
                    getActivity(),
                    BuildConfig.APPLICATION_ID + ".provider",
                    photoFile);

            callCameraApplicationIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);

            // The following is a new line with a trying attempt
            callCameraApplicationIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);

            Logger.getAnonymousLogger().info("Calling the camera App by intent");

            // The following strings calls the camera app and wait for his file in return.
            startActivityForResult(callCameraApplicationIntent, CAMERA_PIC_REQUEST);
清单文件在下面

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

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />


    <uses-feature
        android:name="android.hardware.camera.any"
        android:required="true" />
    <uses-feature
        android:name="android.hardware.camera.autofocus"
        android:required="false" />
    <!--
    Allows Glide to monitor connectivity status and restart failed requests if users go from a
    a disconnected to a connected network state.
    -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <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=".LoginActivity"
            android:label="Login"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">

        </activity>

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>
    </application>

</manifest>
它以前工作得很好。我可能犯了一些愚蠢的错误,因为我只是一个学习者,只需要借助互联网。

根据:

注意:如果应用程序以M及以上为目标,并声明使用未授予的Manifest.permission.CAMERA权限,则尝试使用此操作将导致SecurityException

在任何API级别上使用ACTION\u IMAGE\u CAPTURE都不需要相机权限。因此,如果您仅使用ACTION_IMAGE_CAPTURE,则可以删除该行

如果您除了使用ACTION_IMAGE_CAPTURE之外还使用了Camera API,则必须。

按照:

注意:如果应用程序以M及以上为目标,并声明使用未授予的Manifest.permission.CAMERA权限,则尝试使用此操作将导致SecurityException

在任何API级别上使用ACTION\u IMAGE\u CAPTURE都不需要相机权限。因此,如果您仅使用ACTION_IMAGE_CAPTURE,则可以删除该行


如果您除了使用ACTION_IMAGE_CAPTURE之外还使用摄像头API,则必须。

public void onActivityResultrequestCode、int resultCode、Intent data{super.onActivityResultrequestCode、resultCode、data;If resultCode==RESULT_OK{当调试在最后一行出现错误时…所以,没有更多的SecurityException?StackOverflow上的每个问题都应该只包含一个问题,所以如果您现在走得更远,遇到了一个新问题,您应该尝试调试它,然后用失败的代码问一个新问题。public void on ActivityResultant requestCode,int resultCode,Intent data{super.onActivityResultrequestCode,resultCode,data;if resultCode==RESULT\u OK{当调试在最后一行出现错误时…所以,不再出现SecurityException?StackOverflow上的每个问题都应该只包含一个问题,所以如果您现在走得更远,遇到一个新问题,您应该尝试调试它,然后用失败的代码问一个新问题。