Android(在外部SD卡上写入):java.io.IOException:权限被拒绝 [英] Android (Write on external SD-Card): java.io.IOException: Permission denied

查看:873
本文介绍了Android(在外部SD卡上写入):java.io.IOException:权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我要在外部SD卡(名为/storage/B9BE-18A6)上创建文件时,出现拒绝权限"错误.

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

我知道自Android M以来,您必须以编程方式请求写入权限.因此,我从Arpit Patel(

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)

我不知道为什么我仍然没有这样做的权限. 你们还有其他解决方案,可以在SD卡上创建文件吗?

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?

用于创建文件的代码

    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)

解决方案 感谢@greenapps带给我正确的方法. 解决方案在 https://developer.android.com/上有很好的记录. guide/topics/providers/document-provider.html

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

推荐答案

SD卡是只读的.所以你不会写.您只能写入SD卡上特定于应用程序的目录.在您的情况下,将是

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

如果要在整个SD卡上写入,则File类和FileOutputStream将不起作用.然后,您必须使用Storage Access Framework.

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.

这篇关于Android(在外部SD卡上写入):java.io.IOException:权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆