-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6f1394
commit 60250d3
Showing
6 changed files
with
85 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using Android.Content; | ||
using Android.Provider; | ||
using ShortDev.Microsoft.ConnectedDevices.NearShare; | ||
using Environment = Android.OS.Environment; | ||
|
||
namespace Nearby_Sharing_Windows; | ||
|
||
internal static class FileUtils | ||
{ | ||
public static CdpFileProvider CreateNearShareFileFromContentUriAsync(this ContentResolver contentResolver, AndroidUri contentUri) | ||
{ | ||
var fileName = contentResolver.QueryContentName(contentUri); | ||
|
||
using var fd = contentResolver.OpenAssetFileDescriptor(contentUri, "r") ?? throw new IOException("Could not open file"); | ||
var stream = fd.CreateInputStream() ?? throw new IOException("Could not open input stream"); | ||
|
||
return CdpFileProvider.FromStream(fileName, stream); | ||
} | ||
|
||
public static string QueryContentName(this ContentResolver resolver, AndroidUri contentUri) | ||
{ | ||
using var returnCursor = resolver.Query(contentUri, new[] { IOpenableColumns.DisplayName }, null, null, null) ?? throw new InvalidOperationException("Could not open content cursor"); | ||
returnCursor.MoveToFirst(); | ||
return returnCursor.GetString(0) ?? throw new IOException("Could not query content name"); | ||
} | ||
|
||
public static Stream CreateDownloadFile(this Activity activity, string fileName, ulong size) | ||
{ | ||
var filePath = Path.Combine(activity.GetDownloadDirectory().FullName, fileName); | ||
return File.Create(filePath); | ||
} | ||
|
||
public static DirectoryInfo GetDownloadDirectory(this Activity activity) | ||
{ | ||
var publicDownloadDir = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads)?.AbsolutePath; | ||
DirectoryInfo downloadDir = new(publicDownloadDir ?? Path.Combine(activity.GetExternalMediaDirs()?.FirstOrDefault()?.AbsolutePath ?? "/sdcard/", "Download")); | ||
if (!downloadDir.Exists) | ||
downloadDir.Create(); | ||
|
||
return downloadDir; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters