0

When I Try open a file I have openFd: java.io.FileNotFoundException: open failed: EACCES (Permission denied) . I do this :

This is my provider

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

I put this in manifest

    <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>

I do this :

    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;
    }

And I have see in log in console : openFd: java.io.FileNotFoundException: open failed: EACCES (Permission denied) PdfLoader: Can't load file (doesn't open) Display Data [PDF : b2921c40-5fe2-457b-8700-e0fe26bde42c.pdf] +FileOpenable, uri:

kiki Kala2
  • 399
  • 5
  • 19

1 Answers1

0

Use FileDescriptor for FileOutputStream, this fixed my problem:

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();
}
Divy Soni
  • 824
  • 9
  • 22