Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android在创建文件时崩溃_Java_Android_File - Fatal编程技术网

Java android在创建文件时崩溃

Java android在创建文件时崩溃,java,android,file,Java,Android,File,我正在做一个音频应用程序, 但在尝试创建PCM文件时崩溃,代码如下: public class RecordPCM { public void record() { Log.d("mensaje","Recording testeo started"); int frequency = 11025; //int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO; int chan

我正在做一个音频应用程序, 但在尝试创建PCM文件时崩溃,代码如下:

public class RecordPCM {

public void record() {

    Log.d("mensaje","Recording testeo started");



    int frequency = 11025;
    //int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;

    int channelConfiguration = AudioFormat.CHANNEL_IN_MONO; //cambiado por el de arriba por deprecado


    int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/reverseme.pcm");




    // Delete any previous recording.
    if (file.exists()) {
    file.delete();
    Log.d("mensaje","file exists!!!");

    }


    // Create the new file.
    try {
    file.createNewFile();
    } catch (IOException e) {
    throw new IllegalStateException("Failed to create :::: " + file.toString());
    }

    try {
    // Create a DataOuputStream to write the audio data into the saved file.
    OutputStream os = new FileOutputStream(file);
    BufferedOutputStream bos = new BufferedOutputStream(os);
    DataOutputStream dos = new DataOutputStream(bos);

    // Create a new AudioRecord object to record the audio.
    int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
    AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, 
    frequency, channelConfiguration, 
    audioEncoding, bufferSize);

    short[] buffer = new short[bufferSize]; 
    audioRecord.startRecording();


    boolean isRecording = false; //metido para arreglar

    while (isRecording) {
    int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
    for (int i = 0; i < bufferReadResult; i++)
    dos.writeShort(buffer[i]);
    }


    audioRecord.stop();
    dos.close();

    } catch (Throwable t) {
    Log.d("mensaje","Recording Failed");
    }

}

public void copio(){
    Log.d("mensaje","Recording testeo");

}
}
所以

  • 是什么让我的应用程序崩溃

  • 为什么我的logCat显示为?问号

  • 谢谢

    导致“崩溃”的原因是应用程序在尝试创建文件(但失败)时抛出am
    IllegalStateException
    。为什么会出现
    IOException
    是任何人的猜测。你把证据扔掉了!(提示:使用包含异常参数的
    IllegalStateException
    构造函数…并传递
    e

    您的logcat输出中的问号可以解释为:

    ?:??: W/?(?): FATAL EXCEPTION: main
    ?:??: W/?(?): java.lang.IllegalStateException: Could not execute method of the activity
    ?:??: W/?(?):   at android.view.View$1.onClick(View.java:3597)
    ?:??: W/?(?):   at android.view.View.performClick(View.java:4202)
    ?:??: W/?(?):   at android.view.View$PerformClick.run(View.java:17340)
    ?:??: W/?(?):   at android.os.Handler.handleCallback(Handler.java:725)
    ?:??: W/?(?):   at android.os.Handler.dispatchMessage(Handler.java:92)
    ?:??: W/?(?):   at android.os.Looper.loop(Looper.java:137)
    ?:??: W/?(?):   at android.app.ActivityThread.main(ActivityThread.java:5039)
    ?:??: W/?(?):   at java.lang.reflect.Method.invokeNative(Native Method)
    ?:??: W/?(?):   at java.lang.reflect.Method.invoke(Method.java:511)
    ?:??: W/?(?):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    ?:??: W/?(?):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    ?:??: W/?(?):   at dalvik.system.NativeStart.main(Native Method)
    ?:??: W/?(?): Caused by: java.lang.reflect.InvocationTargetException
    ?:??: W/?(?):   at java.lang.reflect.Method.invokeNative(Native Method)
    ?:??: W/?(?):   at java.lang.reflect.Method.invoke(Method.java:511)
    ?:??: W/?(?):   at android.view.View$1.onClick(View.java:3592)
    ?:??: W/?(?):   ... 11 more
    ?:??: W/?(?): Caused by: java.lang.IllegalStateException: Failed to create :::: /storage/emulated/0/reverseme.pcm
    ?:??: W/?(?):   at com.hyper.reverspeech.RecordPCM.record(RecordPCM.java:48)
    ?:??: W/?(?):   at com.hyper.reverspeech.ReverSpeechActivity.buttonRecPressed(ReverSpeechActivity.java:20)