AudioRecorder不再工作了。 FileNotFoundException:权限被拒绝

时间:2012-01-25 16:21:26

标签: android audio-recording filenotfoundexception permission-denied

我的应用程序中有一个录音机类。它以前运行良好,在我的HTC Desire上工作正常,但现在不适用于我的LG。它不再将音频文件保存到所需的路径。我认为手机上的一些设置会绊倒它,但我可能错了。

writeAudioDataFileFileOutputStream块中filename实例化try-catch时出现错误

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();
            }
    }

错误日志:

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)

1 个答案:

答案 0 :(得分:1)

对于较新的版本,您需要明确的WRITE_EXTERNAL_STORAGE权限。

此外,您不能写入外部存储设备的根目录。因此,尝试创建一个文件夹,然后写入该文件夹。