Pregunta

I have an audio recorder class in my application. It worked fine previously and works fine on my HTC Desire but now doesn't work on my LG. It's no longer saving the audio file to the required path. I'm thinking it's some setting on the phone that's tripping it up, but I'm probably wrong.

An error occurs when writeAudioDataFile instantiates the FileOutputStream with filename in the try-catch block

private String getTempFilename(){
            String filepath = Environment.getExternalStorageDirectory().getPath();
            File file = new File(filepath,AUDIO_RECORDER_FOLDER);

            if(!file.exists()){
                    file.mkdirs();
            }

            File tempFile = new File(filepath,AUDIO_RECORDER_TEMP_FILE);

            if(tempFile.exists()){
                    tempFile.delete();
            }

            return (file.getAbsolutePath() + "/" + AUDIO_RECORDER_TEMP_FILE);
    }

private void writeAudioDataToFile(){
            byte data[] = new byte[bufferSize];
            String filename = getTempFilename();
            FileOutputStream os = null;

            try {
                    os = new FileOutputStream(filename);
            } catch (FileNotFoundException e) {
                    System.out.println("error");
                    e.printStackTrace();
            }

            int read = 0;

            if(null != os){
                    while(isRecording){

                            read = recorder.read(data, 0, bufferSize);

                            if(AudioRecord.ERROR_INVALID_OPERATION != read){
                                    try {                               
                                        os.write(data);

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

                    try {
                            os.close();
                    } catch (IOException e) {
                            e.printStackTrace();
                    }
            }
    }

private void stopRecording(){
            if(null != recorder){
                    isRecording = false;

                    recorder.stop();
                    recorder.release();

                    recorder = null;
                    recordingThread = null;
            }

            copyWaveFile(getTempFilename(),getFilename());
            deleteTempFile();
    }

private void copyWaveFile(String inFilename,String outFilename){
            FileInputStream in = null;
            FileOutputStream out = null;
            long totalAudioLen = 0;
            long totalDataLen = totalAudioLen + 36;
            long longSampleRate = RECORDER_SAMPLERATE;
            int channels = 2;
            long byteRate = RECORDER_BPP * RECORDER_SAMPLERATE * channels/8;

            byte[] data = new byte[bufferSize];

            try {
                    in = new FileInputStream(inFilename);
                    out = new FileOutputStream(outFilename);
                    totalAudioLen = in.getChannel().size();
                    totalDataLen = totalAudioLen + 36;

                    //AppLog.logString("File size: " + totalDataLen);

                    WriteWaveFileHeader(out, totalAudioLen, totalDataLen,
                                    longSampleRate, channels, byteRate);

                    while(in.read(data) != -1){
                            out.write(data);
                    }

                    in.close();
                    out.close();
                    GlobalVar appState = ((GlobalVar)getApplicationContext());
                    appState.addAudioFile(outFilename);
            } catch (FileNotFoundException e) {
                    e.printStackTrace();
            } catch (IOException e) {
                    e.printStackTrace();
            }
    }

Error Log:

01-25 15:46:08.143: W/System.err(1828): java.io.FileNotFoundException: /mnt/sdcard/GeneralGUI2/Study1/Music/record_temp.raw (Permission denied)
01-25 15:46:08.143: W/System.err(1828):     at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
01-25 15:46:08.143: W/System.err(1828):     at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
01-25 15:46:08.143: W/System.err(1828):     at java.io.FileInputStream.<init>(FileInputStream.java:82)
01-25 15:46:08.143: W/System.err(1828):     at java.io.FileInputStream.<init>(FileInputStream.java:134)
01-25 15:46:08.143: W/System.err(1828):     at mfc.generalguixapi8.AudioActivity2.copyWaveFile(AudioActivity2.java:239)
01-25 15:46:08.143: W/System.err(1828):     at mfc.generalguixapi8.AudioActivity2.stopRecording(AudioActivity2.java:217)
01-25 15:46:08.143: W/System.err(1828):     at mfc.generalguixapi8.AudioActivity2.access$1(AudioActivity2.java:206)
01-25 15:46:08.153: W/System.err(1828):     at mfc.generalguixapi8.AudioActivity2$2.onClick(AudioActivity2.java:126)
01-25 15:46:08.153: W/System.err(1828):     at android.view.View.performClick(View.java:2408)
01-25 15:46:08.153: W/System.err(1828):     at android.view.View$PerformClick.run(View.java:8816)
01-25 15:46:08.153: W/System.err(1828):     at android.os.Handler.handleCallback(Handler.java:587)
01-25 15:46:08.153: W/System.err(1828):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 15:46:08.153: W/System.err(1828):     at android.os.Looper.loop(Looper.java:123)
01-25 15:46:08.153: W/System.err(1828):     at android.app.ActivityThread.main(ActivityThread.java:4627)
01-25 15:46:08.153: W/System.err(1828):     at java.lang.reflect.Method.invokeNative(Native Method)
01-25 15:46:08.153: W/System.err(1828):     at java.lang.reflect.Method.invoke(Method.java:521)
01-25 15:46:08.153: W/System.err(1828):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-25 15:46:08.153: W/System.err(1828):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-25 15:46:08.153: W/System.err(1828):     at dalvik.system.NativeStart.main(Native Method)
¿Fue útil?

Solución

For the newer versions, you need an explicit WRITE_EXTERNAL_STORAGE permission.

Also, you are not allowed to write to the root of the external storage device. So, try creating a folder, and then write into that folder.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top