调用相机应用程序(权限拒绝:启动 Intent )

Posted

技术标签:

【中文标题】调用相机应用程序(权限拒绝:启动 Intent )【英文标题】:Calling the Camera Apps (Permission Denial: starting Intent ) 【发布时间】:2020-03-29 21:56:15 【问题描述】:

我的 Fragment 正在尝试启动 Camera Activity,但似乎有一些相机权限问题,

在调试时,它只运行到代码行以下

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 ProcessRecord8920903 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>

以前运行良好。我可能犯了一些愚蠢的错误,因为我只是一个学习者,只能借助互联网。

【问题讨论】:

【参考方案1】:

根据ACTION_IMAGE_CAPTURE documentation:

注意:如果您的应用针对 M 及以上版本并声明使用未授予的Manifest.permission.CAMERA 权限,则尝试使用此操作将导致SecurityException

您无需相机权限即可在任何 API 级别上使用 ACTION_IMAGE_CAPTURE。因此,如果你只使用ACTION_IMAGE_CAPTURE,你可以去掉&lt;uses-permission android:name="android.permission.CAMERA" /&gt;这一行。

如果您在使用 ACTION_IMAGE_CAPTURE 之外还使用相机 API,则必须使用 request the permission at runtime。

【讨论】:

public void onActivityResult(int requestCode, int resultCode, Intent data) super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) 调试时出现在最后一行并发生错误... 那么,没有更多的SecurityException了吗? *** 上的每个问题都应该只包含一个问题,因此如果您现在更进一步并遇到新问题,您应该尝试调试它,然后用失败的代码提出一个新问题。

以上是关于调用相机应用程序(权限拒绝:启动 Intent )的主要内容,如果未能解决你的问题,请参考以下文章

权限被拒绝后可以访问照片库和相机

Android Intent FileProvider“权限被拒绝”在谷歌地球中打开KML文件?

Android - 在没有提示的情况下拒绝相机权限

Android“权限拒绝:无法使用相机”

权限拒绝:广播 Intent act=android.provider.Telephony.SMS_RECEIVED

android studio怎么调用相机