为什么 SELinux 拒绝我的 Android 应用对其自身文件的读取权限?
Why is SELinux denying my Android app read permissions to its own files?
我正在构建的 Android 应用程序难以从我的应用程序目录中读取任何文件,包括内部存储(使用 filesDir
)。
因为它是内部存储,所以我不需要将任何 read/write 权限添加到我的清单中,但为了排除故障,我添加了:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
此外,我的目标设备是从 Marshmallow 开始的,所以我也成功地实现了运行时权限(在不同的 activity 中)...所以任何类型的权限都不应该成为问题。
这是我的 Kotlin 代码,在 Activity 启动后,它应该开始播放 MP3 文件。 (fileName
被前面的activity 传递)
class RecordingManager : AppCompatActivity() {
var audioPlayer: MediaPlayer? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_recording_manager)
actionBar?.title = getString(R.string.recording_manager_actionbar_title)
supportActionBar?.title = getString(R.string.recording_manager_actionbar_title)
actionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
val typedValue = TypedValue()
theme.resolveAttribute(R.attr.colorPrimary, typedValue, true)
val color = typedValue.data
supportActionBar?.setBackgroundDrawable(ColorDrawable(color))
val recordingsDir = File(filesDir, "recordings")
val fileName = intent.getStringExtra("fileName")
txtTitle.text = fileName
val audioFile = File(recordingsDir, fileName!!)
audioFile.setReadable(true)
audioFile.setWritable(true)
var mediaPlayer: MediaPlayer? = MediaPlayer.create(this, Uri.parse(audioFile.absolutePath))
mediaPlayer?.start() // no need to call prepare(); create() does that for you
}
读取Logcat,我可以清楚地看到MP3文件存在,但是MediaPlayer无法读取它:
2019-08-05 14:03:21.360 993-20629/? E/ClearFileSource: Failed to open file '/data/user/0/dev.oliverm.materialvoicerecorder/files/recordings/recording2019-08-0323:35:55.97500.mp3'. (Permission denied)
2019-08-05 14:03:21.360 993-20629/? E/GenericSource: Failed to create data source!
2019-08-05 14:03:21.357 993-993/? W/generic: type=1400 audit(0.0:566): avc: denied { read } for name="0" dev="sda45" ino=524291 scontext=u:r:mediaserver:s0 tcontext=u:object_r:system_data_file:s0 tclass=lnk_file permissive=0
2019-08-05 14:03:21.360 20593-20610/dev.oliverm.materialvoicerecorder E/MediaPlayerNative: error (1, -2147483648)
2019-08-05 14:03:21.361 20593-20593/dev.oliverm.materialvoicerecorder D/MediaPlayer: create failed:
这一行就在这里:
W/generic: type=1400 audit(0.0:566): avc: denied { read } for name="0" dev="sda45" ino=524291
让我相信这是一个 SELinux 问题。
我已经使用多个模拟器和设备对此进行了测试。我用我自己的 Pixel phone 来测试它,我用了多个模拟器(M、N、O、P、Q beta)都无济于事。
我不知道为什么 SELinux 不允许我的应用程序从我的内部应用程序目录中读取文件。非常感谢任何帮助。
Uri.parse(audioFile.absolutePath)
audioFile.absolutePath
不是 URI,它是路径。 URI 将以方案 (file:) 开头。路径从目录开始。因此,您将非 URI 解析为 URI,以异常结尾。使用 Uri.fromFile(audioFile)
代替
更新
我终于可以做更多的工作了,适用于我的代码如下:
audioPlayer = MediaPlayer()
audioPlayer?.setDataSource("file://"+audioFile.absoluteFile)
audioPlayer?.prepare()
audioPlayer?.start()
我最好的猜测是 audioPlayer
是在 onCreate 之外创建的,并且没有设置为 MediaPlayer()
。现在我已经明确地将 audioPlayer
设置为 MediaPlayer()
,它现在似乎可以正常工作。
也可能对其他人有帮助,它有助于解决使用 MediaPlayer
播放本地文件的问题
如果大家有更好的解释,欢迎在下方留下你的答案,我会标为正确的。
我正在构建的 Android 应用程序难以从我的应用程序目录中读取任何文件,包括内部存储(使用 filesDir
)。
因为它是内部存储,所以我不需要将任何 read/write 权限添加到我的清单中,但为了排除故障,我添加了:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
此外,我的目标设备是从 Marshmallow 开始的,所以我也成功地实现了运行时权限(在不同的 activity 中)...所以任何类型的权限都不应该成为问题。
这是我的 Kotlin 代码,在 Activity 启动后,它应该开始播放 MP3 文件。 (fileName
被前面的activity 传递)
class RecordingManager : AppCompatActivity() {
var audioPlayer: MediaPlayer? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_recording_manager)
actionBar?.title = getString(R.string.recording_manager_actionbar_title)
supportActionBar?.title = getString(R.string.recording_manager_actionbar_title)
actionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
val typedValue = TypedValue()
theme.resolveAttribute(R.attr.colorPrimary, typedValue, true)
val color = typedValue.data
supportActionBar?.setBackgroundDrawable(ColorDrawable(color))
val recordingsDir = File(filesDir, "recordings")
val fileName = intent.getStringExtra("fileName")
txtTitle.text = fileName
val audioFile = File(recordingsDir, fileName!!)
audioFile.setReadable(true)
audioFile.setWritable(true)
var mediaPlayer: MediaPlayer? = MediaPlayer.create(this, Uri.parse(audioFile.absolutePath))
mediaPlayer?.start() // no need to call prepare(); create() does that for you
}
读取Logcat,我可以清楚地看到MP3文件存在,但是MediaPlayer无法读取它:
2019-08-05 14:03:21.360 993-20629/? E/ClearFileSource: Failed to open file '/data/user/0/dev.oliverm.materialvoicerecorder/files/recordings/recording2019-08-0323:35:55.97500.mp3'. (Permission denied)
2019-08-05 14:03:21.360 993-20629/? E/GenericSource: Failed to create data source!
2019-08-05 14:03:21.357 993-993/? W/generic: type=1400 audit(0.0:566): avc: denied { read } for name="0" dev="sda45" ino=524291 scontext=u:r:mediaserver:s0 tcontext=u:object_r:system_data_file:s0 tclass=lnk_file permissive=0
2019-08-05 14:03:21.360 20593-20610/dev.oliverm.materialvoicerecorder E/MediaPlayerNative: error (1, -2147483648)
2019-08-05 14:03:21.361 20593-20593/dev.oliverm.materialvoicerecorder D/MediaPlayer: create failed:
这一行就在这里:
W/generic: type=1400 audit(0.0:566): avc: denied { read } for name="0" dev="sda45" ino=524291
让我相信这是一个 SELinux 问题。
我已经使用多个模拟器和设备对此进行了测试。我用我自己的 Pixel phone 来测试它,我用了多个模拟器(M、N、O、P、Q beta)都无济于事。
我不知道为什么 SELinux 不允许我的应用程序从我的内部应用程序目录中读取文件。非常感谢任何帮助。
Uri.parse(audioFile.absolutePath)
audioFile.absolutePath
不是 URI,它是路径。 URI 将以方案 (file:) 开头。路径从目录开始。因此,您将非 URI 解析为 URI,以异常结尾。使用 Uri.fromFile(audioFile)
代替
更新
我终于可以做更多的工作了,适用于我的代码如下:
audioPlayer = MediaPlayer()
audioPlayer?.setDataSource("file://"+audioFile.absoluteFile)
audioPlayer?.prepare()
audioPlayer?.start()
我最好的猜测是 audioPlayer
是在 onCreate 之外创建的,并且没有设置为 MediaPlayer()
。现在我已经明确地将 audioPlayer
设置为 MediaPlayer()
,它现在似乎可以正常工作。
MediaPlayer
如果大家有更好的解释,欢迎在下方留下你的答案,我会标为正确的。