Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fileprovider fails to open file with write permission #19722

Open
guidoDimasi opened this issue Jan 5, 2024 · 7 comments
Open

fileprovider fails to open file with write permission #19722

guidoDimasi opened this issue Jan 5, 2024 · 7 comments
Labels
area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info platform/android 🤖 t/bug Something isn't working

Comments

@guidoDimasi
Copy link

guidoDimasi commented Jan 5, 2024

Description

hi, i'm building an app that download and store file inside the android file system (into a subfolder of /documents).
after downloading it i need to open selected file with the best aviable app (ex: if a file has .xlsx extension it should open with excel app) and into the app it should be able to modify and save in the same path.
the problem is that if my application target the sdk v33 the file opens but it remanin in what it seems read-only mode because if the user try to save the changes excel promt to the "save as" tab.
instead if my application target sdk less then 24 i could open the file without using the file provider (and it opens with write permission and i can save the changes).

whta i'm missing to use the 33sdk and the file provider?

Steps to Reproduce

  1. Crate maui app

  2. add this code to the androidmanifest:

<application android:allowBackup="true" android:icon="@mipmap/appicon" android:supportsRtl="true" android:usesCleartextTraffic="true" android:label="DimasiGestioneDocumentale">
<provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_path" />
</provider>
</application>
  1. add the provider path file inside the android/resources/xml with this text:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
  1. add a custom android service with the method:
public async void OpenFile(FileInfo fileInfo)
{
         Java.IO.File myDir = new(fileInfo.Directory.FullName);
         myDir.Mkdir();
         Java.IO.File file = new(myDir, fileInfo.Name);
         file.SetReadable(true);
         file.SetExecutable(true);
         file.SetWritable(true);
         if (!file.Exists()) return;
         if (!file.CanRead()) return;
         if (!file.CanWrite()) return;
 
         var fileUri = CustomFileProvider.GetUriForFile(Android.App.Application.Context, Android.App.Application.Context.PackageName + ".provider", file);
 
         fileUri.BuildUpon().AppendQueryParameter("grant_read_uri_permission", "true").Build();
         fileUri.BuildUpon().AppendQueryParameter("grant_write_uri_permission", "true").Build();
 
         var intent = new Intent(Intent.ActionView);
         intent.AddFlags(ActivityFlags.NewTask);
         intent.AddFlags(ActivityFlags.GrantReadUriPermission | ActivityFlags.GrantWriteUriPermission);
 
         string extension = MimeTypeMap.GetFileExtensionFromUrl(fileInfo.Name);
         string mimeType = GetMimeType(extension);

         intent.SetDataAndType(fileUri, mimeType);
         Android.App.Application.Context.StartActivity(intent);
}
  1. download the file, save it in a folder like "/storage/emulated/0/Documents/MyAppaDocuments/Asset/TestExcel.xlsx" with succes
  2. pass the fileinfo to the android service

the file opens but it seems readonly

Link to public reproduction project repository

No response

Version with bug

8.0.3

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

android sdk 33

Did you find any workaround?

there is a workaround:

  1. target the sdk v23
  2. pass the file path without using the FileProvider

intent.SetDataAndType(Android.Net.Uri.FromFile(file), mimeType);

Relevant log output

No response

@guidoDimasi guidoDimasi added the t/bug Something isn't working label Jan 5, 2024
@SupermindPT
Copy link

Hi there. Android 11 introduced changes to how applications handle files. Applications have a reserved directory where they can create or read files. If you want to read or open files outside of the reserved directory, you need to declare the MANAGE_ALL_FILES permission in the manifest.

Read more here:
MANAGE_ALL_FILES permission
Android 11 scoped storage

@guidoDimasi
Copy link
Author

guidoDimasi commented Jan 8, 2024

my application already declare MANAGE_ALL_FILES, in fact the application write file inside the shared /documents folder of the system (the one reachable from every app, i can use a folder explorer to view the file even without my app)
the problem is that i wanna launch excel (or any other app for the desired extension) and have the ability to modify and save the file.

@guidoDimasi
Copy link
Author

i've tagged this issue as a bug because i think i'm using the right Intent command and maybe there is something inside MAUI that doesnt work as expected. if not i could use some help to refind the Intent command.

@SupermindPT
Copy link

SupermindPT commented Jan 8, 2024

Typically you would use the Launcher API to launch the apropriate app for the file extension.

If the problem is that you can't save the changes from the external Excel app that is not related to MAUI.

@jsuarezruiz jsuarezruiz added area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info platform/android 🤖 labels Jan 8, 2024
@guidoDimasi
Copy link
Author

the laouncher API documentation for open file with third party app write:

await Launcher.Default.OpenAsync(new OpenFileRequest(popoverTitle, new ReadOnlyFile(file)));

so i avoided it because i need to open and edit the file.
without the launcher my solution was to write an intent following android documentation

@SupermindPT
Copy link

I would search the documentation of the Excel app you are using. Maybe you need to invoke some Uri.

It is very likely newer versions of Android are causing what you describe due to new security restrictions.

@guidoDimasi
Copy link
Author

guidoDimasi commented Jan 8, 2024

i've follow many online resoruces, the file uri seems correct "content://my.software.app/documents/file.xlsx" (in fact the file can be viewed).
maybe there is something with manifest, the file provider or the intent command that on maui need to be wrtitten in some specific way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info platform/android 🤖 t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants