繁体   English   中英

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

[英]Calling the Camera Apps (Permission Denial: starting Intent )

我的片段正在尝试启动相机活动,但似乎存在一些相机权限问题,

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

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>

它以前工作正常。 我可能犯了一些愚蠢的错误,因为我只是一个仅靠互联网帮助的学习者。

根据ACTION_IMAGE_CAPTURE文档

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

在任何 API 级别上使用ACTION_IMAGE_CAPTURE都不需要相机权限。 因此,如果您只使用ACTION_IMAGE_CAPTURE ,则可以删除<uses-permission android:name="android.permission.CAMERA" />

如果除了使用ACTION_IMAGE_CAPTURE之外还使用相机 API,则必须 在运行时请求权限

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM