简体   繁体   English

Android:无法使用bufferedReader读取文件,而在adb shell中运行的cat命令运行正常

[英]Android: unable to read a file using bufferedReader whereas cat command run in adb shell works fine

I am trying to read a system file using bufferedReader in Android API=23 but it gives the following error: open failed: EACCES (Permission denied) . 我正在尝试使用Android API = 23中的bufferedReader读取系统文件,但出现以下错误: 打开失败:EACCES(权限被拒绝)

The file is read only. 该文件是只读的。 On the other hand, 'cat' command run in adb shell just works fine. 另一方面,在adb shell中运行的“ cat”命令也可以正常工作。

Also, File.exists() returns true for my file whereas File.canRead() returns false . 另外, File.exists()对我的文件返回true ,而File.canRead()返回false Is it something related to the administrative/root access? 它与管理/根访问权限有关吗? Does adb shell give you root access to system? adb shell是否可以让您以root身份访问系统?

PS:I have granted Read/Write permissions to the app ( as per API 23 ) PS:我已授予该应用程序的读/写权限( 根据API 23

Android added new permission model for Android 6.0 (Marshmallow). Android为Android 6.0(棉花糖)添加了新的权限模型。

http://www.captechconsulting.com/blogs/runtime-permissions-best-practices-and-how-to-gracefully-handle-permission-removal http://www.captechconsulting.com/blogs/runtime-permissions-best-practices-and-how-to-gracefully-handle-permission-removal

So you have to check Runtime Permission : 因此,您必须检查Runtime Permission:

What Are Runtime Permissions? 什么是运行时权限?

With Android 6.0 Marshmallow, Google introduced a new permission model that allows users to better understand why an application may be requesting specific permissions. Google通过Android 6.0 Marshmallow引入了新的权限模型,该模型使用户可以更好地理解应用程序为何可能要求特定权限。 Rather than the user blindly accepting all permissions at install time, the user is now prompted to accept permissions as they become necessary during application use. 现在,不再提示用户在安装时盲目接受所有权限,而是提示用户接受在应用程序使用过程中必要时所必需的权限。

When to Implement the New Model? 何时实施新模型?

it doesn't require full support until you choose to target version 23 in your application. 在您选择在应用程序中定位版本23之前,不需要全面支持。 If you are targeting version 22 or below, your application will request all permissions at install time just as it would on any device running an OS below Marshmallow. 如果您的目标版本为22或更低版本,则您的应用程序将在安装时请求所有权限,就像在运行低于棉花糖的操作系统的任何设备上一样。

This information is taken from here : 此信息从这里获取:

Please check How to implement from this link : 请从此链接检查如何实施:

http://www.captechconsulting.com/blogs/runtime-permissions-best-practices-and-how-to-gracefully-handle-permission-removal http://www.captechconsulting.com/blogs/runtime-permissions-best-practices-and-how-to-gracefully-handle-permission-removal

If your file exists but can not read, please make sure the followings: 如果您的文件存在但无法读取,请确保以下内容:
- Permission. -许可。 Because you can read other files as you said, it may not a problem. 因为您可以按照您所说的读取其他文件,所以这可能不是问题。
- It is not folder . - 不是文件夹 check it by file.isDirectory() . 通过file.isDirectory()检查它。
- Check your file path . - 检查文件路径 For example, it should not start like that: file:/... or /home/storage//th (when you mistakenly append with two dashes). 例如,它不应该这样开头: file:/.../home/storage//th (当您错误地附加两个破折号时)。

I guess the problem was very much unique to my case. 我想这个问题是我的情况非常独特的。 The file was a system file and my app was not given the Permission to read it. 该文件是系统文件,我的应用未获得读取该文件的许可。 It was rectified after I signed my app as a system app . 将我的应用程序签名系统应用程序后,该问题已得到纠正。

Probable other solution might be if the app will have root permissions to read such file. 可能的其他解决方案可能是该应用程序具有读取此类文件的root权限。

Thanks everyone for your suggestions and answers!! 谢谢大家的建议和回答!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何运行 hdfs cat 命令并使用 Java 读取 output 文件 - How to run a hdfs cat command and read output file using Java Android应用程序外壳“无此文件或目录”,在文件资源管理器和adb中很好用 - Android app shell “no such file or directory”, fine in a file explorer and adb Android:adb shell,找不到命令 adb - Android : adb shell, no command adb found 无法使用FileReader和BufferedReader读取Java中的文本文件 - Unable to read text file in Java using FileReader and BufferedReader 使用FileReader和BufferedReader无法在Java中读取文本文件,可能是原因? - Unable to Read Text File in Java using FileReader and BufferedReader, possible reasons? 使用 BufferedReader 读取文本文件 - Using BufferedReader to read Text File 无法通过BufferedReader读取文本文件 - Unable to Read a Text File via BufferedReader 使用ADB时,Android Studio“找不到文件”,桌面版本有效 - Android studio “File not found” when using ADB, Desktop version works 使用BufferedReader或Scanner读取txt文件。 在我的示例中,仅扫描仪有效。 为什么? - Using BufferedReader or Scanner to read a txt file. in my example only the Scanner works. WHY? 使用BufferedReader.read()处理'file'的末尾 - Dealing with end of 'file' using BufferedReader.read()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM