打开失败:EACCES(权限被拒绝)ANDROID 11
Posted
技术标签:
【中文标题】打开失败:EACCES(权限被拒绝)ANDROID 11【英文标题】:open failed: EACCES (Permission denied) ANDROID 11 【发布时间】:2021-12-23 16:03:30 【问题描述】:我正在将 base64 文件转换为 pdf,所以首先我将 base64 转换为字节数组,然后将其写入 FileOutputStream。我的问题是当我尝试存储它然后用 pdf 查看器打开它时出现此错误:
Faild to generate pdf from base64: /storage/emulated/0/Download/conclusions.pdf: open failed: EACCES (Permission denied)
我还授予它工作的必要权限,因为我使用文件提供程序,因为该应用程序在 android 11 上运行。这是我使用的代码:
活动:
private fun generatePDFFromBase64(base64: String?, fileName: String?)
try
val decodedBytes: ByteArray = Base64.decode(base64, Base64.DEFAULT)
val fos = FileOutputStream(fileName?.let getFilePath(it) )
fos.write(decodedBytes)
fos.flush()
fos.close()
fileName?.let openDownloadedPDF(it)
catch (e: IOException)
Log.e("TAG", "Faild to generate pdf from base64: $e.localizedMessage")
private fun openDownloadedPDF(fileName: String)
val file = File(getFilePath(fileName))
if (file.exists())
val path: Uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
FileProvider.getUriForFile(requireContext(), BuildConfig.APPLICATION_ID + ".provider", file)
else
Uri.fromFile(file)
generalFile = file
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(path, "application/pdf")
intent.flags = Intent.FLAG_ACTIVITY_NO_HISTORY or Intent.FLAG_GRANT_READ_URI_PERMISSION
val chooserIntent = Intent.createChooser(intent, "Open with")
try
startActivity(chooserIntent)
catch (e: ActivityNotFoundException)
Log.e("TAG", "Failed to open PDF $e.localizedMessage")
private fun getFilePath(filename: String): String
val file =
File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).path)
if (!file.exists())
file.mkdirs()
return file.absolutePath.toString() + "/" + filename + ".pdf"
Android 清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:name="university"
android:allowBackup="false"
android:icon="$appIcon"
android:roundIcon="$appIconRound"
android:label="@string/app_name"
android:supportsRtl="true"
android:hardwareAccelerated="true"
android:theme="@style/Theme.University"
android:usesCleartextTraffic="true">
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="$applicationId.provider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths"
tools:replace="android:resource" />
</provider>
文件路径:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<cache-path
name="cache"
path="." />
<external-cache-path
name="external_cache"
path="." />
<files-path
name="files"
path="." />
</paths>
【问题讨论】:
My problem is when I try to store it adn then open it with pdf viewer
或者是在保存的时候。或者是在打开的时候。请准确。什么时候?
【参考方案1】:
打开您的应用信息应用权限在显示所有权限后单击所有权限在问题解决后单击以启用。
【讨论】:
以上是关于打开失败:EACCES(权限被拒绝)ANDROID 11的主要内容,如果未能解决你的问题,请参考以下文章
BitmapFactory:无法解码流:java.io.FileNotFoundException:打开失败:Android Q 上的 EACCES(权限被拒绝)
打开失败:EACCES(权限被拒绝)并尝试在空对象引用上调用虚拟方法“int android.graphics.Bitmap.getWidth()”
io.FileNotFoundException:打开失败:EACCES(权限被拒绝)