private bool CreateTestFileStructureAtLocation() { try { Android.Net.Uri dirUri = DocumentsContract.BuildDocumentUriUsingTree( _userSelectedDirectory, DocumentsContract.GetTreeDocumentId(_userSelectedDirectory) ); for (int i = 0; i < 10; ++i) { // create directory var newDir = DocumentsContract.CreateDocument( this.ContentResolver, dirUri, DocumentsContract.Document.MimeTypeDir, $"Directory {i + 1}" ); if (newDir == null) { throw new Exception("Unable to create directory at location!"); } for (int j = 0; j < 100; ++j) { var newFile = DocumentsContract.CreateDocument( this.ContentResolver, newDir, "text/plain", $"file{j + 1}.txt"); if (newFile == null) { throw new Exception("Unable to create file at location!"); } } } } catch (Exception ex) { // set our text view to display the error var timeElapsedTV = FindViewById <TextView>(Resource.Id.timeElapsedTV); SetTextViewOnError(timeElapsedTV, ex); } return(true); }
public CreateStreamResult CreateStream(string name, StreamAccess access, StreamShare share, int bufferSize = 0) { var resolver = Context.ContentResolver; var androidUri = DocumentsContract.CreateDocument(resolver, AndroidUri, null, name); var fs = OpenStream(androidUri, StreamMode.Open, access, share, bufferSize); var ret = new CreateStreamResult() { Stream = fs, EntryBase = new StorageEntryBase { Name = name, LastWriteTime = DateTime.Now, Size = 0, IsStorage = false, Uri = AndroidUriToUri(androidUri) } }; return(ret); }
public CreateStorageResult CreateStorage(string name) { var docUri = DocumentsContract.CreateDocument(Context.ContentResolver, AndroidUri, DocumentsContract.Document.MimeTypeDir, name); if (docUri == null) { throw new Exception($"Could not create storage. Uri: {Uri}, name: {name}"); } var ret = new CreateStorageResult() { Storage = new SafStorgeProvider(Context, docUri, name), Entry = new StorageEntryBase { Name = name, LastWriteTime = DateTime.Now, Size = 0, IsStorage = true, Uri = AndroidUriToUri(docUri), } }; return(ret); }
/// <summary> /// Creates a directory under the directory represented as the uri in the argument. /// </summary> /// <param name="uri">The uri of the directory under which a new directory is created.</param> /// <param name="directoryName">The directory name of a new directory.</param> void CreateDirectory(Android.Net.Uri uri, string directoryName) { var contentResolver = Activity.ContentResolver; var docUri = DocumentsContract.BuildDocumentUriUsingTree(uri, DocumentsContract.GetTreeDocumentId(uri)); var directoryUri = DocumentsContract .CreateDocument(contentResolver, docUri, DocumentsContract.Document.MimeTypeDir, directoryName); if (directoryUri != null) { Log.Info(TAG, string.Format( "Created directory : {0}, Document Uri : {1}, Created directory Uri : {2}", directoryName, docUri, directoryUri)); Toast.MakeText(Activity, string.Format("Created a directory [{0}]", directoryName), ToastLength.Short).Show(); } else { Log.Warn(TAG, string.Format("Failed to create a directory : {0}, Uri {1}", directoryName, docUri)); Toast.MakeText(Activity, string.Format("Failed to created a directory [{0}] : ", directoryName), ToastLength.Short).Show(); } }