Skip to content

Fix crash when creating documents in an opened folder on android #18651

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

Merged
merged 2 commits into from
Apr 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ public AndroidStorageFolder(Activity activity, AndroidUri uri, bool needsExterna
public Task<IStorageFile?> CreateFileAsync(string name)
{
var mimeType = MimeTypeMap.Singleton?.GetMimeTypeFromExtension(MimeTypeMap.GetFileExtensionFromUrl(name)) ?? "application/octet-stream";
var newFile = DocumentsContract.CreateDocument(Activity.ContentResolver!, Uri, mimeType, name);
var treeUri = DocumentsContract.BuildDocumentUriUsingTree(Uri, DocumentsContract.GetTreeDocumentId(Uri));
var newFile = DocumentsContract.CreateDocument(Activity.ContentResolver!, treeUri!, mimeType, name);
if(newFile == null)
{
return Task.FromResult<IStorageFile?>(null);
Expand All @@ -170,7 +171,8 @@ public AndroidStorageFolder(Activity activity, AndroidUri uri, bool needsExterna

public Task<IStorageFolder?> CreateFolderAsync(string name)
{
var newFolder = DocumentsContract.CreateDocument(Activity.ContentResolver!, Uri, DocumentsContract.Document.MimeTypeDir, name);
var treeUri = DocumentsContract.BuildDocumentUriUsingTree(Uri, DocumentsContract.GetTreeDocumentId(Uri));
var newFolder = DocumentsContract.CreateDocument(Activity.ContentResolver!, treeUri!, DocumentsContract.Document.MimeTypeDir, name);
if (newFolder == null)
{
return Task.FromResult<IStorageFolder?>(null);
Expand Down Expand Up @@ -205,7 +207,8 @@ async Task DeleteContents(AndroidStorageFolder storageFolder)
}
}

DocumentsContract.DeleteDocument(Activity.ContentResolver!, storageFolder.Uri);
var treeUri = DocumentsContract.BuildDocumentUriUsingTree(storageFolder.Uri, DocumentsContract.GetTreeDocumentId(storageFolder.Uri));
DocumentsContract.DeleteDocument(Activity.ContentResolver!, treeUri!);
}
}

Expand Down
Loading