简体   繁体   中英

Android (Write on external SD-Card): java.io.IOException: Permission denied

I get a "Permission denied" error, when I want to create a file on my external SD-Card (named /storage/B9BE-18A6).

I know that you have to ask for the write permission programmatically since Android M. So I inserted the solution from Arpit Patel ( Android 6.0 Marshmallow. Cannot write to SD Card )

I don't know why I still haven't the permissions to do it. Do you guys have another solution that I can create files on my SD-Card?

Code for creating the file

    FloatingActionButton fab_new_file = (FloatingActionButton) rLayoutFrgEmpresas.findViewById(R.id.fab_menu_item_file);
    fab_new_file.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            int permission = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if (permission != PackageManager.PERMISSION_GRANTED) {
                Log.v("Permission: " ,"Denied");

                if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                        Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    builder.setMessage("Permission to access the SD-CARD is required")
                            .setTitle("Permission required");

                    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int id) {
                            Log.i(TAG, "Clicked");
                            makeRequest();
                        }
                    });

                    AlertDialog dialog = builder.create();
                    dialog.show();

                } else {
                    makeRequest();
                }
            }else{
                Log.v("Permission: " ,"Granted");
                File file = new File(textView_currentPath.getText() + "/" + "testfile.txt");
                Log.v("filepatch: ", ""+file);
                if (!file.exists()) {
                    Log.v("Does "+file+" exists?", "No");
                    try {
                        file.createNewFile();
                        getFilesFromDir(textView_currentPath.getText() + "", textView_currentPath.getText() + "");
                        Log.v("File "+file,"has been created!");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    });

LogCat

04-11 16:31:40.709 23404-23404/spicysoftware.com.phonemanager D/ViewRootImpl@ab22b33[MainActivity]: ViewPostImeInputStage processPointer 0
04-11 16:31:40.773 23404-23404/spicysoftware.com.phonemanager D/ViewRootImpl@ab22b33[MainActivity]: ViewPostImeInputStage processPointer 1
04-11 16:31:40.783 23404-23404/spicysoftware.com.phonemanager V/Permission:: Granted
04-11 16:31:40.783 23404-23404/spicysoftware.com.phonemanager V/filepatch:: /storage/B9BE-18A6/testfile.txt
04-11 16:31:40.783 23404-23404/spicysoftware.com.phonemanager V/Does /storage/B9BE-18A6/testfile.txt exists?: No
04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err: java.io.IOException: Permission denied
04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at java.io.File.createNewFile(File.java:948)
04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at spicysoftware.com.phonemanager.StorageFragment$2.onClick(StorageFragment.java:187)
04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at android.view.View.performClick(View.java:6207)
04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at android.view.View$PerformClick.run(View.java:23639)
04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at android.os.Looper.loop(Looper.java:154)
04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6688)
04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

Solution Thanks to @greenapps for bringing me on the right way. Solution is very well documentated at https://developer.android.com/guide/topics/providers/document-provider.html

        Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);

        String mimeType = "text/plain";
        String filename = "file"+System.currentTimeMillis() + ".txt";
        intent.setType(mimeType);
        intent.putExtra(Intent.EXTRA_TITLE, filename);
        startActivityForResult(intent, CREATE_REQUEST_CODE);

The SD card is read only. So you cannot write. You only can write to an app specific directory on the SD card. In your case it would be

/storage/B9BE-18A6/Android/data/spicysoftware.com.phonemanager

If you want to write on the whole SD card then the File class and FileOutputStream will not do. You have to use the Storage Access Framework then.

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