EACCES Permission Denied in Kitkat while writing to SD card

旧城冷巷雨未停 提交于 2019-12-01 10:45:10

问题


i am trying to copy file to micro sd card from internal storge of mobile and getting error Eacces. I searched alot but now where found clear solution. Is is possible or not to copy in non rooted / rooted Kitkat devices.

Here is my code:

package jss.kitkatfile;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button copy;
        copy = (Button) findViewById(R.id.Copy);
        copy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    write();
                } catch (IOException e) {
                    e.printStackTrace();
                }

    public void write()
            throws IOException {
        String dirpath = Environment.getExternalStorageDirectory() + "/WhatsApp/Media";
        String filename = "A.mp4";


        File file = new File(dirpath,filename);
        System.out.println(file);


            FileInputStream fileInputStream = new FileInputStream(file);
            byte[] filelength = new byte[fileInputStream.available()];
            fileInputStream.read(filelength);

            //String dest = Environment.getExternalStorageDirectory() + "/Music";
            String dest = "/storage/MicroSD/Android";
            File outfile = new File(dest, filename);
            FileOutputStream fileOutputStream = new FileOutputStream(outfile);

            fileOutputStream.write(filelength);

    }
}

Error as below:

java.io.FileNotFoundException: /storage/MicroSD/Android/A.mp4: open failed: EACCES (Permission denied)
W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:409)
W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
W/System.err:     at jss.kitkatfile.MainActivity.write(MainActivity.java:69)
W/System.err:     at jss.kitkatfile.MainActivity$1.onClick(MainActivity.java:27)
W/System.err:     at android.view.View.performClick(View.java:4478)
W/System.err:     at android.view.View$PerformClick.run(View.java:18698)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:733)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at android.os.Looper.loop(Looper.java:149)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5257)
W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
W/System.err:     at dalvik.system.NativeStart.main(Native Method)
W/System.err: Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
W/System.err:     at libcore.io.Posix.open(Native Method)
W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:393)
W/System.err:   ... 15 more

回答1:


You do not have arbitrary write access to removable storage on Android 4.4+.

You are welcome to:

  • Use getExternalFilesDirs(), getExternalCacheDirs(), and getExternalMediaDirs() methods on Context. If these return 2+ items, the second and subsequent ones are locations on removable storage, unique to your app, where you can read and write.

  • Use ACTION_OPEN_DOCUMENT, ACTION_CREATE_DOCUMENT, and ACTION_OPEN_DOCUMENT_TREE. These represent the Storage Access Framework and bring up the equivalent of "open file", "save as", and "open directory" dialogs that you might be used to from desktop operating systems. However, what you get back is a Uri representing the selected location, and you would use ContentResolver and DocumentFile to work with that location.

  • On Android 6.0+, you can use the StorageVolume APIs to request access to an entire removable storage volume. However, you will still be working with Uri values, so this is the equivalent of using ACTION_OPEN_DOCUMENT_TREE, where the user chose the root of a removable storage volume.




回答2:


did you add

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

in android manifest file ?

Also it looks like the file /storage/MicroSD/Android/A.mp4.
Are you sure the path is correct and that the file A.mp4 exists ?



来源:https://stackoverflow.com/questions/40564293/eacces-permission-denied-in-kitkat-while-writing-to-sd-card

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!