创建新文件Android时权限被拒绝

时间:2014-07-18 10:03:34

标签: android permissions audio-recording

我正在研究Android的录音机应用程序,我有这个问题。 我有两个测试设备,1个运行4.3另一个运行4.0。到目前为止,我使用4.3运行的设备进行测试,我没有错误。但今天我在4.0设备上测试了,我收到了这个错误:

07-18 11:36:17.910: W/System.err(3320): java.io.FileNotFoundException: /mnt/sdcard/MyPadRecords/My Record 5.mp3: open failed: EACCES (Permission denied)
07-18 11:36:17.910: W/System.err(3320):     at libcore.io.IoBridge.open(IoBridge.java:419)
07-18 11:36:17.910: W/System.err(3320):     at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
07-18 11:36:17.910: W/System.err(3320):     at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
07-18 11:36:17.910: W/System.err(3320):     at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
07-18 11:36:17.910: W/System.err(3320):     at android.media.MediaRecorder.prepare(MediaRecorder.java:629)
07-18 11:36:17.910: W/System.err(3320):     at soundmaniacs.com.MainClass$2.onClick(MainClass.java:138)
07-18 11:36:17.910: W/System.err(3320):     at android.view.View.performClick(View.java:3534)
07-18 11:36:17.910: W/System.err(3320):     at android.view.View$PerformClick.run(View.java:14263)
07-18 11:36:17.910: W/System.err(3320):     at android.os.Handler.handleCallback(Handler.java:605)
07-18 11:36:17.910: W/System.err(3320):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 11:36:17.910: W/System.err(3320):     at android.os.Looper.loop(Looper.java:137)
07-18 11:36:17.910: W/System.err(3320):     at android.app.ActivityThread.main(ActivityThread.java:4441)
07-18 11:36:17.910: W/System.err(3320):     at java.lang.reflect.Method.invokeNative(Native Method)
07-18 11:36:17.910: W/System.err(3320):     at java.lang.reflect.Method.invoke(Method.java:511)
07-18 11:36:17.910: W/System.err(3320):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-18 11:36:17.910: W/System.err(3320):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-18 11:36:17.920: W/System.err(3320):     at dalvik.system.NativeStart.main(Native Method)
07-18 11:36:17.920: W/System.err(3320): Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
07-18 11:36:17.920: W/System.err(3320):     at libcore.io.Posix.open(Native Method)
07-18 11:36:17.920: W/System.err(3320):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
07-18 11:36:17.920: W/System.err(3320):     at libcore.io.IoBridge.open(IoBridge.java:403)
07-18 11:36:17.920: W/System.err(3320):     ... 16 more
07-18 11:36:17.920: E/MediaRecorder(3320): start called in an invalid state: 4
07-18 11:36:17.920: D/AndroidRuntime(3320): Shutting down VM
07-18 11:36:17.920: W/dalvikvm(3320): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
07-18 11:36:17.920: E/AndroidRuntime(3320): FATAL EXCEPTION: main
07-18 11:36:17.920: E/AndroidRuntime(3320): java.lang.IllegalStateException
07-18 11:36:17.920: E/AndroidRuntime(3320):     at android.media.MediaRecorder.start(Native Method)
07-18 11:36:17.920: E/AndroidRuntime(3320):     at soundmaniacs.com.MainClass$2.onClick(MainClass.java:144)
07-18 11:36:17.920: E/AndroidRuntime(3320):     at android.view.View.performClick(View.java:3534)
07-18 11:36:17.920: E/AndroidRuntime(3320):     at android.view.View$PerformClick.run(View.java:14263)
07-18 11:36:17.920: E/AndroidRuntime(3320):     at android.os.Handler.handleCallback(Handler.java:605)
07-18 11:36:17.920: E/AndroidRuntime(3320):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 11:36:17.920: E/AndroidRuntime(3320):     at android.os.Looper.loop(Looper.java:137)
07-18 11:36:17.920: E/AndroidRuntime(3320):     at android.app.ActivityThread.main(ActivityThread.java:4441)
07-18 11:36:17.920: E/AndroidRuntime(3320):     at java.lang.reflect.Method.invokeNative(Native Method)
07-18 11:36:17.920: E/AndroidRuntime(3320):     at java.lang.reflect.Method.invoke(Method.java:511)
07-18 11:36:17.920: E/AndroidRuntime(3320):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-18 11:36:17.920: E/AndroidRuntime(3320):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-18 11:36:17.920: E/AndroidRuntime(3320):     at dalvik.system.NativeStart.main(Native Method)

我有权录制:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

这是我的代码:

  File FolderCreator = new File(Environment.getExternalStorageDirectory()
                + "/MyPadRecords");
        File FileCreator;

创建:

if (android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED)) {
        }

        if (!FolderCreator.exists()) {
            if (FolderCreator.mkdir()) {
            }

        }

然后是简单的录音代码:

@Override
                public void onClick(View v) {

                    if (recording == false) {

                        SharedPreferences FileExtensionPreference = PreferenceManager
                                .getDefaultSharedPreferences(getApplicationContext());
                        int savedvalue = FileExtensionPreference.getInt(
                                "FileExtensionValue", num);
                        num = savedvalue;
                        num++;
                        SharedPreferences.Editor editor = PreferenceManager
                                .getDefaultSharedPreferences(
                                        getApplicationContext()).edit();
                        editor.putInt("FileExtensionValue", num);
                        editor.commit();
                        FileCreator = new File(FolderCreator, "My Record " + num
                                + ".mp3");

                        recorder = new MediaRecorder();
                        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                        recorder.setMaxDuration(700000);
                        recorder.setMaxFileSize(40000000);
                        recorder.setOutputFile(FileCreator.getAbsolutePath());

                        try {
                            recorder.prepare();
                        } catch (IllegalStateException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        recorder.start();
                        recording = true;
                        Toast.makeText(MainClass.this, "Recording started",
                                Toast.LENGTH_SHORT).show();
                        record.setImageResource(R.drawable.selector_stoprecord);
                    } else {
                        recorder.stop();
                        recorder.reset();
                        recorder.release();
                        record.setImageResource(R.drawable.selector_record);
                        recording = false;
                        Toast.makeText(
                                MainClass.this,
                                "Recording stopped, file is being saved to"
                                        + FileCreator, Toast.LENGTH_LONG).show();
                    }

                }
            });

第144行是recorder.start(); 为什么我收到此错误?我的代码中有什么问题吗?

1 个答案:

答案 0 :(得分:2)

某些设备在通过USB连接到PC并同时写入外部存储器时遇到问题。它不适合调试,但这个问题依赖于制造商。只需从usb中禁用该设备即可。