Aditya
Aditya

Reputation: 154

Android app closes when opened, but it doesn't crash

I was working on an Android studio application, and it was working until a certain point. I added in a MediaRecorder to record audio, and wrote the code for recording and stopping the recording. When I tried to test out code, everything compiled and downloaded correctly. However, when the app opened, it would immediately close. When checking the logcat it seemed that there were no obvious errors.

Logcat:

2019-06-24 15:11:19.302 30658-30658/? I/art: Late-enabling -Xcheck:jni
2019-06-24 15:11:19.495 30658-30658/com.example.visualizercopy W/System: ClassLoader referenced unknown path: /data/app/com.example.visualizercopy-1/lib/arm64
2019-06-24 15:11:19.515 30658-30658/com.example.visualizercopy I/InstantRun: starting instant run server: is main process
2019-06-24 15:11:19.732 30658-30658/com.example.visualizercopy I/Timeline: Timeline: Activity_launch_request intent: act=android.content.pm.action.REQUEST_PERMISSIONS pkg=com.google.android.packageinstaller time:351586171
2019-06-24 15:11:19.887 30658-30722/com.example.visualizercopy I/Adreno: QUALCOMM build                   : d3015e9, I25dc76dc3f
    Build Date                       : 01/26/17
    OpenGL ES Shader Compiler Version: XE031.09.00.03
    Local Branch                     : 
    Remote Branch                    : refs/tags/AU_LINUX_ANDROID_LA.BF64.1.2.3_RB1.07.00.00.258.011
    Remote Branch                    : NONE
    Reconstruct Branch               : NOTHING
2019-06-24 15:11:19.895 30658-30722/com.example.visualizercopy I/OpenGLRenderer: Initialized EGL, version 1.4
2019-06-24 15:11:19.895 30658-30722/com.example.visualizercopy D/OpenGLRenderer: Swap behavior 1
2019-06-24 15:11:20.071 30658-30658/com.example.visualizercopy D/Editor: setInputTypeforClipTray(): 0
2019-06-24 15:11:20.157 30658-30658/com.example.visualizercopy D/Editor: setInputTypeforClipTray(): 0
2019-06-24 15:11:20.161 30658-30658/com.example.visualizercopy D/Editor: hideClipTrayIfNeeded() TextView is focused!! hideClipTray()
2019-06-24 15:11:20.336 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: reportFullscreenMode on inexistent InputConnection
2019-06-24 15:11:20.481 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
2019-06-24 15:11:20.483 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
2019-06-24 15:11:20.484 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
2019-06-24 15:11:20.523 30658-30658/com.example.visualizercopy D/Editor: hideClipTrayIfNeeded() TextView is focused!! hideClipTray()
2019-06-24 15:11:20.612 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
2019-06-24 15:11:20.615 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: reportFullscreenMode on inexistent InputConnection
2019-06-24 15:11:20.615 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
2019-06-24 15:11:20.620 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: finishComposingText on inactive InputConnection

I tried to look online for a solution to my problem, but non of them seemed to work. How can I fix this? -EDIT- Code that I added:

public class MainActivity extends Activity implements ESenseConnectionListener {
        private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;
        private static String FILE_NAME_MIC="recording.3gp";
        private MediaRecorder recorder;
        private boolean permissionToRecordAccepted = false;
        private String [] permissions = {Manifest.permission.RECORD_AUDIO};

        @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();
       }
    private void startRecording() {
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setOutputFile(FILE_NAME_MIC);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            recorder.prepare();
        } catch (IOException e) {
            Log.e("Record", "prepare() failed");
        }

        recorder.start();
    }
    private void stopRecording(){
        recorder.stop();
        recorder.release();
        recorder = null;
    }

Gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    defaultConfig {
        applicationId "com.example.visualizercopy"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0-beta01'
    implementation 'com.android.support.constraint:constraint-layout:2.0.0-beta1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

Manifest:

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

    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

    <dist:module dist:instant="true" />

    <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=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Upvotes: 0

Views: 142

Answers (1)

TheScribbler2019
TheScribbler2019

Reputation: 187

You can`t just pass a random name to your MediaRecorder like:

private static String FILE_NAME_MIC="recording.3gp";

FILE_NAME_MIC needs to be a representation of a location inside your storage.

final static String FILE_NAME_MIC = Environment.getExternalStorageDirectory() + "/" + "MyAppDirectory"+ "/recording.mp3";

Now we know that we want to write/read something to/from our device but we can`t just do that. We need the permission to do so. First you need to add the permissions to your Manifest like:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Next we need to ask for multiple permissions. This link can teach everything you need to know and more:

MyMediaRecorder

Conclusion: Your FILE_NAME_MIC and your permissions are the source of your "crash".

Try and delete:

if (!permissionToRecordAccepted ) finish();

and see what happens.

Upvotes: 2

Related Questions