在请求权限之前,我的应用程序崩溃了

时间:2017-07-30 14:29:53

标签: android permissions android-permissions

第一次运行应用程序时崩溃(logcat上没有任何内容)。然后你可以看到权限请求,并在你给予权限后,你可以打开应用程序,它工作正常。

public class MainActivity extends AppCompatActivity {

    String [] PERMISSIONS = {Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
    int PERMISSION_ALL  = 1;

    Button record;
    Button play;
    Button stop;

    MediaPlayer mediaPlayer;
    MediaRecorder mediaRecorder;

    String outPutFile;

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

        //---------------------
        //asking for permission
        //---------------------

        if(!hasPermissions(this, PERMISSIONS)){
            ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
        }
        record = (Button) findViewById(R.id.recordbutton);
        play = (Button) findViewById(R.id.playbutton);
        stop = (Button) findViewById(R.id.stopbutton);

        if (outPutFile==null){
            play.setEnabled(false);
        }

        stop.setEnabled(false);

        outPutFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";

        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mediaRecorder.setOutputFile(outPutFile);

        //---------------------
        //recording
        //---------------------

        record.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    mediaRecorder.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                    //Toast.makeText(getApplicationContext(),"error", Toast.LENGTH_LONG).show();

                }
                mediaRecorder.start();
                record.setEnabled(false);
                stop.setEnabled(true);

                Toast.makeText(getApplicationContext(),"recording", Toast.LENGTH_LONG).show();
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mediaRecorder.stop();
                mediaRecorder.release();
                mediaRecorder = null;
                stop.setEnabled(false);
                play.setEnabled(true);

                Toast.makeText(getApplicationContext(), "Audio recorded", Toast.LENGTH_LONG).show();
            }
        });
        //---------------------
        //playing the record
        //---------------------

        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mediaPlayer = new MediaPlayer();

                try {
                    mediaPlayer.setDataSource(outPutFile);
                    mediaPlayer.prepare();

                    Toast.makeText(getApplicationContext(), "Audio Playing", Toast.LENGTH_LONG).show();


                } catch (Exception e) {

                }
                mediaPlayer.start();

                record.setEnabled(true);
            }
        });


    }

    public static boolean hasPermissions(Context context, String... permissions) {
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
            for (String permission : permissions) {
                if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }
        return true;
    }
}

为什么?

的logcat -

07-30 19:03:03.812 4491-4491/com.example.tsuryohananov.voicerecorder E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                       Process: com.example.tsuryohananov.voicerecorder, PID: 4491
                                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tsuryohananov.voicerecorder/com.example.tsuryohananov.voicerecorder.MainActivity}: java.lang.RuntimeException: setAudioSource failed.
                                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                                           at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                                           at android.os.Looper.loop(Looper.java:164)
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:6540)
                                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                                           at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                                        Caused by: java.lang.RuntimeException: setAudioSource failed.
                                                                                           at android.media.MediaRecorder.setAudioSource(Native Method)
                                                                                           at com.example.tsuryohananov.voicerecorder.MainActivity.onCreate(MainActivity.java:64)
                                                                                           at android.app.Activity.performCreate(Activity.java:6980)
                                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                                                                                           at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                                           at android.os.Looper.loop(Looper.java:164) 
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:6540) 
                                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                                           at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

1 个答案:

答案 0 :(得分:0)

我移动了那些代码:

    mediaRecorder = new MediaRecorder();
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    mediaRecorder.setOutputFile(outPutFile);

    record.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mediaRecorder = new MediaRecorder();
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mediaRecorder.setOutputFile(outPutFile);
            try {
                mediaRecorder.prepare();

            } catch (IOException e) {
                e.printStackTrace();

            }
            record.setEnabled(false);
            stop.setEnabled(true);
            mediaRecorder.start();

            Toast.makeText(getApplicationContext(),"recording", Toast.LENGTH_LONG).show();
        }
    });

问题是,首先运行mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

在用户授予权限之前运行。

我认为还有其他方法,例如:

    if (hasPermissions(this, PERMISSIONS)) {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(outPutFile);

    }