I was asked by my college to find errors on why the given app is unable to request write_external_storage permission.
They provided us with a mainActivity.java code for a simple recording application, which I feel is generated by chatGPT
Being new to android development, I have no knowledge about how android’s new permissions work and I’m unable to get a workaround.
if there are any other errors in this code, please point them out.
Please help
I’m attaching the entire code.
package com.example.tryrecord;
import android.Manifest;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.pm.PackageManager;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private static final int RECORD_AUDIO_PERMISSION_CODE = 1;
private static final int WRITE_EXTERNAL_STORAGE_PERMISSION_CODE = 2;
private static final String TAG = "MainActivity";
private Button startButton;
private Button pauseButton;
private MediaRecorder recorder;
private boolean isRecording = false;
private String outputFile = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = findViewById(R.id.startButton);
pauseButton = findViewById(R.id.pauseButton);
startButton.setOnClickListener(v -> {
if (checkRecordAudioPermission()) {
if (checkWriteExternalStoragePermission()) {
startRecording();
} else {
requestWriteExternalStoragePermission();
}
} else {
requestRecordAudioPermission();
}
});
pauseButton.setOnClickListener(v -> stopRecording());
// Ensure pauseButton is initially invisible
pauseButton.setVisibility(View.INVISIBLE);
}
private boolean checkRecordAudioPermission() {
return ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
}
private boolean checkWriteExternalStoragePermission() {
return ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
}
private void requestRecordAudioPermission() {
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.RECORD_AUDIO},
RECORD_AUDIO_PERMISSION_CODE
);
}
private void requestWriteExternalStoragePermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Check if the app is running on Android 6.0 (API level 23) or higher
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// Request the WRITE_EXTERNAL_STORAGE permission
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
WRITE_EXTERNAL_STORAGE_PERMISSION_CODE
);
// The result of this request will be handled in onRequestPermissionsResult method
} else {
// WRITE_EXTERNAL_STORAGE permission already granted, proceed with the recording
startRecording();
}
} else {
// For SDK versions below Android 6.0, permissions are granted at install time
startRecording();
}
}
private void startRecording() {
recorder = new MediaRecorder();
outputFile = getOutputFilePath();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(outputFile);
try {
recorder.prepare();
recorder.start();
isRecording = true;
startButton.setVisibility(View.INVISIBLE);
pauseButton.setVisibility(View.VISIBLE);
Toast.makeText(this, "Recording started", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Recording failed: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
private void stopRecording() {
if (isRecording) {
recorder.stop();
recorder.release();
isRecording = false;
startButton.setVisibility(View.VISIBLE);
pauseButton.setVisibility(View.INVISIBLE);
Toast.makeText(this, "Recording stopped", Toast.LENGTH_SHORT).show();
}
}
private String getOutputFilePath() {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
File audioVoiceDir = new File(getExternalFilesDir(Environment.DIRECTORY_MUSIC), "AudioVoice");
if (!audioVoiceDir.exists()) {
boolean mkdirsResult = audioVoiceDir.mkdirs();
if (!mkdirsResult) {
Log.e(TAG, "Failed to create directory: " + audioVoiceDir.getAbsolutePath());
}
}
return audioVoiceDir.getAbsolutePath() + "/Recording_" + timeStamp + ".3gp";
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == RECORD_AUDIO_PERMISSION_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (checkWriteExternalStoragePermission()) {
startRecording();
} else {
Toast.makeText(this, "Write external storage permission denied", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "Record audio permission denied", Toast.LENGTH_SHORT).show();
}
} else if (requestCode == WRITE_EXTERNAL_STORAGE_PERMISSION_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startRecording();
} else {
Toast.makeText(this, "Write external storage permission denied", Toast.LENGTH_SHORT).show();
}
}
}
}
The permissions in manifest.xml are –
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I tried reading Android manuals, tried chatGPT, tried other websites to check but cannot find a workaround.
Aashutosh Sharan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.