@@ -56,20 +56,61 @@ public static string RemapPathForApi300OrAbove(string path)
5656
5757 public static void Copy ( string from , string to )
5858 {
59- Stream file =
60- AndroidCore . context . ContentResolver . OpenOutputStream ( Uri . Parse ( RemapPathForApi300OrAbove ( to ) ) ) ;
59+ Stream file = GetOutputStream ( to ) ;
6160 StreamWriter sw = new StreamWriter ( file ) ;
6261 sw . Write ( File . ReadAllBytes ( from ) ) ;
6362 sw . Dispose ( ) ;
6463 }
6564
6665 public static void CreateDirectory ( string dir )
6766 {
68- Logger . Log ( RemapPathForApi300OrAbove ( Directory . GetParent ( dir ) . FullName ) ) ;
69- DocumentFile parent = DocumentFile . FromTreeUri ( AndroidCore . context , Uri . Parse ( RemapPathForApi300OrAbove ( Directory . GetParent ( dir ) . FullName ) ) ) ;
67+ DocumentFile parent = GetAccessToFile ( Directory . GetParent ( dir ) . FullName ) ;
7068 Logger . Log ( parent . CanWrite ( ) . ToString ( ) ) ;
7169 parent . CreateDirectory ( Path . GetFileName ( dir ) ) ;
7270 }
71+
72+ /// <summary>
73+ ///
74+ /// </summary>
75+ /// <param name="dir">Expected as /sdcard/Android/data/...</param>
76+ /// <returns></returns>
77+ public static DocumentFile GetAccessToFile ( string dir )
78+ {
79+ Logger . Log ( "Trying to get access to " + dir ) ;
80+ string start = "/sdcard/Android/data/" + CoreService . coreVars . currentApp ;
81+ string diff = dir . Replace ( start + "/" , "" ) ;
82+ string [ ] dirs = diff . Split ( '/' ) ;
83+ DocumentFile startDir = DocumentFile . FromTreeUri ( AndroidCore . context , Uri . Parse ( RemapPathForApi300OrAbove ( start ) . Replace ( "com.android.externalstorage.documents/document/" , "com.android.externalstorage.documents/tree/" ) ) ) ;
84+ DocumentFile currentDir = startDir ;
85+ foreach ( string dirName in dirs )
86+ {
87+ currentDir = currentDir . FindFile ( dirName ) ;
88+ }
89+ return currentDir ;
90+ }
91+
92+ public static Stream GetOutputStream ( string path )
93+ {
94+ DocumentFile directory = GetAccessToFile ( Directory . GetParent ( path ) . FullName ) ;
95+ string name = Path . GetFileName ( path ) ;
96+ if ( directory . FindFile ( name ) != null ) directory . FindFile ( name ) . Delete ( ) ;
97+ return AndroidCore . context . ContentResolver . OpenOutputStream ( directory . CreateFile ( "application/octet-stream" , name ) . Uri ) ;
98+ }
99+
100+ public static void Delete ( string path )
101+ {
102+ DocumentFile directory = GetAccessToFile ( Directory . GetParent ( path ) . FullName ) ;
103+ string name = Path . GetFileName ( path ) ;
104+ if ( directory . FindFile ( name ) != null ) directory . FindFile ( name ) . Delete ( ) ;
105+ }
106+
107+ public static void CreateDirectoryIfNotExisting ( string path )
108+ {
109+ Logger . Log ( "Creating directory " + path + " if it doesn't exist" ) ;
110+ DocumentFile directory = GetAccessToFile ( Directory . GetParent ( path ) . FullName ) ;
111+ string name = Path . GetFileName ( path ) ;
112+ if ( directory . FindFile ( name ) == null ) directory . CreateDirectory ( name ) ;
113+ }
73114 }
74115
75116 public class FolderPermissionCallback : Java . Lang . Object , IActivityResultCallback
@@ -92,6 +133,7 @@ public void OnActivityResult(Object result)
92133 {
93134 if ( activityResult . Data . Data != null )
94135 {
136+ Logger . Log ( activityResult . Data . Data . ToString ( ) ) ;
95137 AndroidCore . context . ContentResolver . TakePersistableUriPermission (
96138 activityResult . Data . Data ,
97139 ActivityFlags . GrantReadUriPermission | ActivityFlags . GrantWriteUriPermission ) ;
0 commit comments