My App with Android 11 doesn't load PDF file

Paulo Silva 141 Reputation points
2021-07-05T17:34:02.217+00:00

I have this lines in my code

var fileName = Guid.NewGuid().ToString();
            localPath = Task.Run(() => dependency.SaveFileToDisk($"{fileName}.pdf", bytes)).Result;

But when i get to these lines it throws an exception

File must be corrupt or not exists

But i have fileName. Is needed some permission in the Andropid Manifest? Look, this line work with Android 10 very well.

This method is the problem

public async Task<string> SaveFileToDisk(string fileName, byte[] bytes)
        {
            if (!Directory.Exists(_rootDir)) Directory.CreateDirectory(_rootDir);

            var filePath = Path.Combine(_rootDir, fileName);

            File.WriteAllBytes(filePath, bytes);

            return filePath;
        }

I need some permission?

EDIT 1

A friend give me this to do

public bool SaveActivity(List<CheckListActivitiesByUser> model)
        {
            bool result = false;

            try
            {
                string fileName = "Global1";     //Global.Storage.FileNameActivity;
                string data = JsonConvert.SerializeObject(model);
                Preferences.Set(fileName, data);
                result = true;
            }
            catch(Exception ex)
            {
                string erro = ex.Message;
            }
            return result;
        }

        public List<CheckListActivitiesByUser> GetActivity()
        {
            List<CheckListActivitiesByUser> result = new List<CheckListActivitiesByUser>();
            try
            {
                string fileName = Global.UsbMassStorageEnabled.FileNameActivity;
                var data = Preferences.Get(fileName, string.Empty);
                if(!string.IsNullOrEmpty(data))
                {
                    result = JsonConvert.DeserializeObject<List<CheckListActivitiesByUser>(data));
                }
            }
            catch(Exception ex)
            {
                string erro = ex.Message;
            }

            return result;
        }

But i don't know what's CheckListActivitiesByUser. This is a model, correct, but what's model?

EDIT2

[Obsolete]
        public async Task<string> SaveFileToDisk(string fileName, byte[] bytes)
        {
            if (!Directory.Exists(_rootDir))
                Directory.CreateDirectory(_rootDir);

            var filePath = Path.Combine(_rootDir, fileName);

            File.WriteAllBytes(filePath, bytes);

            return filePath;
        }

Looking for my code when it is in this line

Directory.CreateDirectory(_rootDir);

I get the error. How can I solve this?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,292 questions
{count} votes

Accepted answer
  1. Alessandro Caliaro 4,181 Reputation points
    2021-07-06T07:59:08.093+00:00

    With android 11 is not possible to use "externalstorage" because of new restrictions:

    storage

    You should ask for a new permission MANAGE_EXTERNAL_STORAGE but it could create problems with Google Play Store

    10467955

    Otherwise try to store the file inside the app internal storage directory
    app-specific

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful