【问题标题】:Android Q openFd: java.io.FileNotFoundException: open failed: EACCES (Permission denied)Android Q openFd:java.io.FileNotFoundException:打开失败:EACCES(权限被拒绝)
【发布时间】:2020-01-14 08:23:35
【问题描述】:

当我尝试打开一个文件时,我有 openFd: java.io.FileNotFoundException: open failed: EACCES (Permission denied) 。我这样做:

这是我的提供者

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="external_files"
        path="Android/data/xxx/files/Download" />
</paths>

我把它放在清单中

    <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="xxx.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths2" />
    </provider>

我这样做:

    public static void parseBase64ToPDFAndNoOpen(String base64, String cardId ,Context context) throws IOException {

        FileOutputStream os;
        String path;
        Uri photoURI = null;
        try {
            photoURI = FileProvider.getUriForFile(context.getApplicationContext(),
                    BuildConfig.APPLICATION_ID + ".provider", createImageFile(context,cardId));
        } catch (IOException e) {
            e.printStackTrace();
        }
        File dwldsPath = new File(photoURI.toString());
//            File dwldsPath = new File(Environment.getExternalStorageDirectory() + "/" + File.separator + cardId + ".pdf");
        if (!dwldsPath.exists()) {
            dwldsPath.createNewFile();
            byte[] pdfAsBytes = Base64.decode(base64, 0);
            os = new FileOutputStream(dwldsPath, false);
            os.write(pdfAsBytes);
            os.flush();
            os.close();
        }
    }

private static File createImageFile(Context context, String name) {
return new File(getDefaultTempFilesPath(context), name + ".pdf");

}

   private static String getDefaultTempFilesPath(Context context) {
        if (context != null) {
            File f = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
            if (f != null)
                return f.getPath();
            else {
                f = context.getFilesDir();
                if (f != null)
                    return f.getPath();
            }
        }
        return null;
    }

我在控制台的日志中看到:openFd: java.io.FileNotFoundException: open failed: EACCES (Permission denied) PdfLoader:无法加载文件(无法打开)显示数据 [PDF : b2921c40-5fe2-457b-8700-e0fe26bde42c.pdf] +FileOpenable, uri:

【问题讨论】:

  • @isaaaaame 不,它对我不起作用
  • 你试过addong &lt;application android:requestLegacyExternalStorage="true" ... &gt; 吗?
  • @isaaaaame 是的,我添加了,我添加了所有权限并授予它
  • EACCES (Permission denied) PdfLoader: PdfLoder?我在您的代码中没有看到 pdf 加载器。也没有任何试图打开要加载的文件的代码。为什么不发布 uri?

标签: android


【解决方案1】:

FileDescriptor 用于FileOutputStream,这解决了我的问题:

public static void parseBase64ToPDFAndNoOpen(String base64, String cardId, Context context) throws IOException {
    FileOutputStream os;
    String path;
    Uri photoURI = null;
    try {
        photoURI = FileProvider.getUriForFile(context.getApplicationContext(),
                BuildConfig.APPLICATION_ID + ".provider", createImageFile(context, cardId));
    } catch (IOException e) {
        e.printStackTrace();
    }
    ParcelFileDescriptor descriptor = context.getContentResolver().openFileDescriptor(photoURI, "rw");
    byte[] pdfAsBytes = Base64.decode(base64, 0);
    os = new FileOutputStream(descriptor.getFileDescriptor());
    os.write(pdfAsBytes);
    os.flush();
    os.close();
}

【讨论】:

    猜你喜欢
    • 2021-12-23
    • 2021-07-02
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    • 2016-06-14
    相关资源
    最近更新 更多