简体   繁体   中英

Permission Denied when creating new file Android

I am working on audio recorder app for android, and I have this problem. I have two test devices, 1 is running on 4.3 other is running on 4.0. So far I was using device running on 4.3 for testing and I got no errors. But today I tested on 4.0 device and I got this error:

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)

I have permissions to record:

<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"/>

And here is my code:

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

On Create:

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

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

        }

And then simple recording code:

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

                }
            });

Line 144 is recorder.start(); Why am I getting this error? Is there something wrong in my code?

Some devices have problems with connecting to the pc via usb and writing to external storage at the same time. It´s bad for debugging, but this issue relies on the manufacturer. Just disable the device from usb and it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM