Editing a file in an external application - readonly

Hi,
I have a series of files with different extensions that have been downloaded and stored in my applications private local storage area.
I then have the requirement that users should be able to open these files in the App of their choosing, make any changes and then Save/overwrite that original file. I have used the code as below however find that in quite a lot of Apps, the files open Read-only and in others, the behaviour is inconsistant e.g. in Xodo, PDF files can be edited and saved no problem whilst jpg files always open readonly.

I therefore wonder, is the code correct and if there is any other way to achieve this to maximise the number of Apps able to open files in write mode?
At the point of download, it is not a problem to have the files stored in a different location however this location is stored in a Sqlite database so that the file can be re-uploaded (with changes) at a later date

    public async Task<string> SaveToLocalUserAttachmentsFolderAsync(Stream dataStream, string userID, string fileName)
    {
        var filePath = string.Format("{0}/{1}/{2}", Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads), userID, fileName);
        Directory.CreateDirectory(Path.GetDirectoryName(filePath));

        Java.IO.File p = new Java.IO.File(filePath);

        if (File.Exists(filePath))
        {
            File.Delete(filePath);
        }

        using (var fileStream = File.OpenWrite(filePath))
        {
            if (dataStream.CanSeek)
            {
                dataStream.Position = 0;
            }

            await dataStream.CopyToAsync(fileStream);

            p.SetWritable(true, false);

            return filePath;
        }
    }


public async Task OpenFileForEditFromPathAsync(string filePath)
    {
        Java.IO.File file = new Java.IO.File(filePath);
        var exists = file.Exists();

        try
        {
            Android.Net.Uri path = Android.Net.Uri.FromFile(file);
            string extension = MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString());
            string mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
            Intent intent = new Intent(Intent.ActionEdit);
            intent.AddFlags(ActivityFlags.ClearWhenTaskReset);
            intent.AddFlags(ActivityFlags.GrantReadUriPermission);
            intent.AddFlags(ActivityFlags.GrantWriteUriPermission);
            intent.AddFlags(ActivityFlags.GrantPersistableUriPermission);


            var uri = FileProvider.GetUriForFile(Android.App.Application.Context, Android.App.Application.Context.PackageName + ".fileprovider", file);

            if (uri != null)
            {
                intent.SetData(uri);
            }

            this.StartActivity(Intent.CreateChooser(intent, "Choose App"));
        }
        catch (Exception ex)
        {
            throw;
        }
    }

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    Could you please post a basic demo so that we can test with it?

  • CaptainRCaptainR Member ✭✭

    Hi, I have attached a simple app which illustrates the issue - the files getting on the device is mocked. When I open the jpg in Xodo, it is readonly whilst when I open the PDF in Xodo, it is writable. I experience this readonly issue in quite a few other apps as well.
    I ideally require a solution/ file location where the files will always open in a writable state such that the user can over-write the existing file

  • CaptainRCaptainR Member ✭✭

    I have heard back from Xodo and images are saved to PDF behind the scenes which is why they appear as read-only.
    There are still unfortunately other apps such as the Gallery app which opens my files read-only / uneditable.

Sign In or Register to comment.