-3

I'm new developer in android development i have two apps in play store but my apps now is not working with android 6 & android 7 i get this messages

runtimeException and NullPointerException

this my main activity code i have problem with:

public class Recored extends Activity {     

Button rec,stop;
private MediaRecorder myRecords;
private String outputFile = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recored);

    stop = (Button)findViewById(R.id.bStop);
    rec = (Button)findViewById(R.id.bRecord);

    stop.setEnabled(false);
    myRecords = new MediaRecorder();
    myRecords.setAudioSource(MediaRecorder.AudioSource.MIC);
    myRecords.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    myRecords.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    Date createdDate = new Date();
    File myDir = new File(Environment.getExternalStorageDirectory()+"/Recordings");
    if (!myDir.exists()){
        if (myDir.mkdir());
    }
        outputFile = myDir.getAbsolutePath() + "/" + createdDate + ".mp3";
        myRecords.setOutputFile(outputFile);

        rec.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    myRecords.prepare();
                    myRecords.start();


                }

                catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
                rec.setEnabled(false);
                stop.setEnabled(true);
                Toast.makeText(getApplicationContext(),"Recording started",Toast.LENGTH_LONG).show();
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myRecords.stop();
                myRecords.release();
                myRecords = null;
                stop.setEnabled(false);
                rec.setEnabled(true);
            }
        });


}

}

AbouLkhair
  • 175
  • 3
  • 16
hazem
  • 5

2 Answers2

0

Did you ask for Run-time permission?

 private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;

  private String[] permissions = {Manifest.permission.RECORD_AUDIO};

// Add this code to OnCreate

ActivityCompat.requestPermissions(this, permissions, REQUEST_RECORD_AUDIO_PERMISSION);

Override this method

  @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST_RECORD_AUDIO_PERMISSION:
                permissionToRecordAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                break;
        }
        if (!permissionToRecordAccepted) finish();

    }
AmmY
  • 1,821
  • 1
  • 20
  • 31
0

Need to handle runtime permission on android 6 & upper version. You have to ask for permission and manage your code according to permission is denied or accepted.

You can refer below link for runtime permission https://developer.android.com/training/permissions/requesting.html

Harshal Shah
  • 427
  • 3
  • 5