android.permission.WRITE_MEDIA_STORAGE错误 [英] android.permission.WRITE_MEDIA_STORAGE error

查看:1697
本文介绍了android.permission.WRITE_MEDIA_STORAGE错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编辑文本文件的应用程序.通过ACTION_OPEN_DOCUMENT活动打开文件时,我能够读写文件:

I am working on an app that edits text files. I am able to r/w files when they are opened via an ACTION_OPEN_DOCUMENT activity:

 Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
 intent.addCategory(Intent.CATEGORY_OPENABLE);
 intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

所有功能都可以在内部和外部SD存储设备上正常运行.

It all works just right on both internal and external SD storage.

我还为android.intent.action.VIEW设置了带有意图过滤器的应用程序.该应用程序显示在Android MyFiles应用程序的选择器"中.从MyFiles返回的Uri看起来与从ACTION_OPEN_DOCUMENT返回的Uri不同,即它具有文件号而不是文件名. 使用MyFiles传递给我的intent.getData()中的Uri,我可以读取文件,但不能将它们写入SD卡(外部存储).但是,我既可以读取文件,也可以将文件写入内部存储.

I have also set up the app with an intent-filter for android.intent.action.VIEW. The app shows up in the "chooser" in the Android MyFiles application. The Uri returned from MyFiles looks different than the one returned from ACTION_OPEN_DOCUMENT, i.e. it has a file number instead of a file name. Using the Uri in intent.getData() that MyFiles passes to me I am able to read the files but not write them to the SD card (external storage). However, I am able to both read and write files to internal storage.

在清单文件中,我定义了以下权限:

In my Manifest file I have the following permissions defined:

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

在Java代码中,我也明确要求获得权限:

In the java code I'm also explicitly asking for the permissions as well:

    int permis = CheckSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);

            if (permis ==  PackageManager.PERMISSION_GRANTED)
            {
                Toast.makeText(getApplicationContext(),"Permits Manifest.permission.WRITE_EXTERNAL_STORAGE", Toast.LENGTH_LONG);
                return true;
            }

            Toast.makeText(getApplicationContext(),"Getting Manifest.permission.WRITE_EXTERNAL_STORAGE", Toast.LENGTH_LONG);

            //android.permission.WRITE_MEDIA_STORAGE

            String[] array = {  Manifest.permission.READ_EXTERNAL_STORAGE,
                                Manifest.permission.WRITE_EXTERNAL_STORAGE
                    };

   ActivityCompat.requestPermissions(thisActivity,array, MY_PERMISSIONS_REQUEST_READ_CONTACTS)

我有一个用于requestPermissions()的结果处理程序,它显示权限已成功授予.

I have a result handler for requestPermissions() and it shows that the permissions were successfully granted.

当我尝试打开文件进行写入时,出现以下异常:

When I try to open the file for writing I get the following exception:

 pfd = MainActivity.this.getContentResolver().openFileDescriptor(uri, "w");
 fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());

异常:外部路径:/storage/0123-4567/Test/123.txt:用户10196或当前进程都没有android.permission.WRITE_MEDIA_STORAGE.

从MyFiles获取文件句柄之后,是否还需要请求其他权限来执行写操作,或者是否需要使用其他一些技术来打开具有完全r/w访问权限的文件?

Is there some other permission that I need to request to do the write, or is there some other technique I need to use to open the file with the full r/w access after I get the file handle from MyFiles?

推荐答案

除非您具有root用户,否则该问题似乎无法解决,请参见 Jorrit的"Chainfire"帖子钟马:

Unless you have root, the problem seems to be unfixable, see this post by Jorrit "Chainfire" Jongma:

因此,如果您购买的Android设备预装有16gb(或8、32,等等)存储,并且还可以使用SD卡,则您的设备已经有两个外部存储.

So, if you have purchased an Android device that came preloaded with 16gb (or 8, or 32, etc) storage and it can also use an SD card, your device has two external storages already.

通常将内部闪存视为主要的外部存储位置,而将SD卡视为辅助外部存储的位置(如果存在内部闪存).您可能会连接的USB记忆棒或硬盘驱动器也将是辅助外部存储.

The internal flash memory would usually be considered the primary external storage location, while the SD card would be considered the secondary external storage location (if internal flash is present). A USB stick or harddrive you can potentially connect would also be secondary external storage.

看来,在较新的Android版本中,Google使得第三方应用程序(未预先装载在设备上,但您是从Android Market/Google Play手动安装或下载的应用程序)无法获得对外部应用程序的写入权限SD卡.

It appears that in newer Android builds, Google is making it impossible for third party apps (apps that were not preloaded on the device, but you manually installed or downloaded from Android Market / Google Play) to gain write access to the external SD card.

[...]

过去,应用会请求"WRITE_EXTERNAL_STORAGE"权限,该权限将授予对所有外部存储(用户/组"sdcard_rw")的写访问权限.显然已更改为仅授予对主外部存储的写访问权限.已经引入了第二个权限,称为"WRITE_MEDIA_STORAGE",该权限将授予对其他外部存储(用户/组"media_rw")的访问权限.

In the past, an app would request the "WRITE_EXTERNAL_STORAGE" permission, which would grant write access to all external storages (user/group "sdcard_rw"). This has apparently been changed to only grant write access to the primary external storage. A second permission has been introduced called "WRITE_MEDIA_STORAGE", which would grant access to the other external storages (user/group "media_rw").

这篇关于android.permission.WRITE_MEDIA_STORAGE错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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