Open a file, get its path, save the file MAUI Android

Влад Тигинян 40 Reputation points
2023-02-22T11:33:30.11+00:00

Hi all. I try to open files, change them and save them in android 11+.

I know that you need permission to access all files. I receive it.

if (Android.OS.Environment.IsExternalStorageManager)
            {
                await DisplayAlert("Android VERSION  R OR ABOVE", "HAVE MANAGE_EXTERNAL_STORAGE GRANTED!", "ОК");
            }
            else
            {
                await DisplayAlert("Android VERSION  R OR ABOVE", "NO HAVE MANAGE_EXTERNAL_STORAGE GRANTED!", "ОК");
                string package = AppInfo.Current.PackageName;
                var uri = Android.Net.Uri.Parse($"package:{package}");
                Platform.CurrentActivity.StartActivityForResult(new Intent
                    (Android.Provider.Settings.ActionManageAppAllFilesAccessPermission, uri), 10);
            }

When opening a file, I get its uri and create a file.

Intent intent = Intent;
var uri = intent.Data;
string uriPath = uri.Path;
Java.IO.File file = new Java.IO.File(uriPath);
string fullFilePath = file.AbsolutePath;
string f = file.Path;  
Launcher.OpenAsync(new OpenFileRequest
{
  File = new ReadOnlyFile(fullFilePath)
});

If I get the path from a file and try to display it, I get an error.

User's image

Tell me how to open files correctly, modify them and save them in the same path?

The MAUI file selector is not suitable for this purpose. It copies the file to the application's cache. In other words, I need to get the text editor to work.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,906 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,580 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,000 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 43,926 Reputation points Microsoft Vendor
    2023-06-28T07:50:38.8566667+00:00

    Hello,

    For opening and saving files, the corresponding components have been updated in MAUI, and you can use these official components directly.

    Please refer to the following documentations:

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.