public void CopyGuideFiles(LocalModule localModule) { if (Environment.ExternalStorageState.Equals(Environment.MediaMounted)) { string curPath = AppStorageManager.GetInstance(mContext).DefaultFolder; if (!AppFileUtil.NeedScopedStorageAdaptation()) { curPath = AppFileUtil.DefaultDocumentDirectory; Java.IO.File file = new Java.IO.File(curPath); if (!file.Exists()) { if (!file.Mkdirs()) { return; } } Java.IO.File sampleFile = new Java.IO.File(curPath + Java.IO.File.Separator + "Sample.pdf"); if (!sampleFile.Exists()) { localModule.CopyFileFromAssertsToTargetFile(sampleFile); } Java.IO.File guideFile = new Java.IO.File(curPath + Java.IO.File.Separator + "complete_pdf_viewer_guide_android.pdf"); if (!guideFile.Exists()) { localModule.CopyFileFromAssertsToTargetFile(guideFile); } } else if (!TextUtils.IsEmpty(curPath)) { Uri uri = AppFileUtil.ToDocumentUriFromPath(curPath); if (AppFileUtil.IsDocumentTreeUri(uri)) { DocumentFile directory = AppStorageManager.GetInstance(mContext).GetExistingDocumentFile(uri); if (directory == null) { return; } string fileName = "Sample.pdf"; DocumentFile file = directory.FindFile(fileName); if (file == null) { file = directory.CreateFile(AppFileUtil.GetMimeType(fileName), fileName); localModule.CopyFileFromAssertsToTargetFile(file); } fileName = "complete_pdf_viewer_guide_android.pdf"; file = directory.FindFile(fileName); if (file == null) { file = directory.CreateFile(AppFileUtil.GetMimeType(fileName), fileName); localModule.CopyFileFromAssertsToTargetFile(file); } } localModule.SetCurrentPath(curPath); } } }
/// <summary> /// liefert das DocumentFile für eine Datei oder ein Verzeichnis (API level 14) /// <para>Wenn das Objekt noch nicht ex., wird es erzeugt. Auch ein notwendiger Pfad wird vollständig erzeugt.</para> /// </summary> /// <param name="storagename">z.B. "primary" oder "19F4-0903"</param> /// <param name="volpath">abs. Pfad im Volume</param> /// <param name="isDirectory">Pfad bezieht sich auf eine Datei oder ein Verzeichnis</param> /// <returns></returns> public DocumentFile GetDocumentFile(string storagename, string volpath, bool isDirectory) { global::Android.Net.Uri rooturi = GetTreeDocumentUri(storagename); DocumentFile document = DocumentFile.FromTreeUri(Activity.ApplicationContext, rooturi); string[] pathelems = volpath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; document != null && i < pathelems.Length; i++) { DocumentFile nextDocument = document.FindFile(pathelems[i]); if (nextDocument == null) { if ((i < pathelems.Length - 1) || isDirectory) { nextDocument = document.CreateDirectory(pathelems[i]); } else { nextDocument = document.CreateFile("", pathelems[i]); } } document = nextDocument; } System.Diagnostics.Debug.WriteLine("AndroidGetDocumentFile(" + storagename + ", " + volpath + ", " + isDirectory.ToString() + ") = " + (document != null && (isDirectory == document.IsDirectory)).ToString()); return(document != null ? (isDirectory == document.IsDirectory ? document : null) : null); }
public async Task <IFile> CreateFileAsync(string desiredName, CreationCollisionOption option, CancellationToken cancellationToken = default(CancellationToken)) { desiredName = await EnsureExistence(desiredName, option, cancellationToken); var newFile = folder.CreateFile(GetMimeType(desiredName), desiredName); return(new AndroidFile(context, newFile, this)); }
public override async Task <StorageFile> CreateFileAsync(string desiredName, Windows.Storage.CreationCollisionOption options, CancellationToken cancellationToken) { return(await Task.Run(async() => { var actualName = desiredName; var existingItem = await TryGetItemAsync(desiredName, cancellationToken); switch (options) { case CreationCollisionOption.ReplaceExisting: if (existingItem is StorageFolder) { throw new UnauthorizedAccessException("There is already a folder with the same name."); } if (existingItem is StorageFile) { // Delete existing file await existingItem.DeleteAsync(); } break; case CreationCollisionOption.FailIfExists: if (existingItem != null) { throw new UnauthorizedAccessException("There is already an item with the same name."); } break; case CreationCollisionOption.OpenIfExists: if (existingItem is StorageFile existingFile) { return existingFile; } if (existingItem is StorageFolder) { throw new UnauthorizedAccessException("There is already a file with the same name."); } break; case CreationCollisionOption.GenerateUniqueName: actualName = await FindAvailableNumberedFileNameAsync(desiredName); break; default: throw new ArgumentOutOfRangeException(nameof(options)); } var extension = IOPath.GetExtension(actualName); var mimeType = MimeTypeService.GetFromExtension(extension); var file = _directoryDocument.CreateFile("", actualName); return StorageFile.GetFromSafDocument(file); }, cancellationToken)); }
private async Task <bool> WriteFileAsync(ContentType accessType, string filePath, string storageLocationBase, byte[] bytes) { switch (accessType) { case ContentType.DirectAccess: await File.WriteAllBytesAsync(filePath, bytes); return(true); case ContentType.StorageFramework: // check file exists var fileExistPath = this.GetPathIfFileExists(accessType, filePath, storageLocationBase); Uri contentPath; if (!fileExistPath.Exists) { var pathUri = Uri.Parse(storageLocationBase); //var treeId = DocumentsContract.GetTreeDocumentId(pathUri) + $"/{filePath}"; var treeId = DocumentsContract.GetTreeDocumentId(pathUri) + $"/{Path.GetDirectoryName(filePath)}"; var newPath = DocumentsContract.BuildDocumentUriUsingTree(pathUri, treeId); DocumentFile newFile = DocumentFile.FromTreeUri(Android.App.Application.Context, newPath); var documentFile = newFile.CreateFile("application/octet-stream", Path.GetFileName(filePath)); contentPath = documentFile.Uri; } else { var baseUriParse = Uri.Parse(storageLocationBase); contentPath = DocumentsContract.BuildDocumentUriUsingTree(baseUriParse, fileExistPath.Path); } var descriptor = AppContentResolver.OpenAssetFileDescriptor(contentPath !, "w"); if (descriptor == null) { throw new Exception($"File descriptor null, tried to open {contentPath}."); } var writeStream = descriptor.CreateOutputStream(); if (!writeStream.CanWrite) { throw new Exception("Cannot write the writeStream."); } await writeStream.WriteAsync(bytes); return(true); default: throw new ArgumentOutOfRangeException(nameof(accessType), accessType, null); } }
/// <inheritdoc/> public async Task <bool> TrySaveFileToPickedFolder(string fileName, byte[] content) { if (_pickedUri == null) { return(false); } try { DocumentFile folder = DocumentFile.FromTreeUri(_context, _pickedUri); string mimeType = URLConnection.GuessContentTypeFromName(fileName); DocumentFile file = folder.CreateFile(mimeType, fileName); using (Stream stream = _context.ContentResolver.OpenOutputStream(file.Uri, "w")) { await stream.WriteAsync(content, 0, content.Length); return(true); } } catch (Exception ex) { return(false); } }
public Task <IFile> CreateFileAsync(string desiredName, CreationCollisionOption option, CancellationToken cancellationToken = default(CancellationToken)) { folder.CreateFile(GetMimeType(desiredName), desiredName); }