Android Torch 应用程序无法在 Marshmallow 中运行

Android Torch App not working in Marshmallow

如果我手动授予权限,它就会起作用。而不是它不起作用。谁能帮我。我应该做什么改变。我是 android 开发的新手。我在 google.

上找不到任何内容

清单:

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

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


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_flashlight_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".MainActivity"
        android:screenOrientation="portrait">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

主要Activity:

package com.plutianclub.flashlight;

import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity {

ImageButton imageButton;

Camera camera;
Camera.Parameters parameters;
boolean isFlash = false;
boolean isOn = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imageButton = (ImageButton) findViewById(R.id.offButton);

    if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
        camera = Camera.open();
        parameters = camera.getParameters();
        isFlash = true;
    }

    imageButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            if (isFlash == true){
                if (isOn == false  ){
                    imageButton.setImageResource(R.drawable.on);
                    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                    camera.setParameters(parameters);
                    camera.startPreview();
                    isOn = true;

                }else {
                    imageButton.setImageResource(R.drawable.off);
                    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                    camera.setParameters(parameters);
                    camera.stopPreview();
                    isOn = false;
                }

            }else {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Error....");
                builder.setMessage("Flashlight is not avilable on this device.");
                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        finish();
                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();

            }
        }
    });
}

@Override
protected void onStop() {
    super.onStop();
    if (camera != null){
        camera.release();
        camera = null;
    }
}
}

来自 Android 6.0 (Marshmallow) Android 要求您在运行时或将要使用该功能时通过代码请求许可。

这里有更多的例子。 https://developer.android.com/training/permissions/requesting.html

低于 Android 6.0 的任何内容都需要,因为您已经在清单中完成了