Exemplo n.º 1
0
 public override async Task DeleteAsync(CancellationToken ct, StorageDeleteOption options)
 {
     await Task.Run(() =>
     {
         _directoryDocument.Delete();
     }, ct);
 }
        public async Task <bool> RemoveFileIfExists(ContentType accessType, string filename, string storageLocationBase)
        {
            var fileIfExists = GetPathIfFileExists(accessType, filename, storageLocationBase);

            if (!fileIfExists.Exists)
            {
                return(false);
            }

            switch (accessType)
            {
            case ContentType.DirectAccess:
                File.Delete(fileIfExists.Path);
                await Task.Delay(50);

                return(true);

            case ContentType.StorageFramework:
                var          doc  = DocumentsContract.BuildDocumentUriUsingTree(Uri.Parse(storageLocationBase), fileIfExists.Path);
                DocumentFile file = DocumentFile.FromTreeUri(Android.App.Application.Context, doc);
                await Task.Delay(50);

                return(file.Delete());

            default:
                throw new ArgumentOutOfRangeException(nameof(accessType), accessType, null);
            }
        }
Exemplo n.º 3
0
        public void DeleteFile(string path)
        {
            Android.Net.Uri uri;

            string auth     = "com.Linaq.LinaqStorage.Android.fileprovider";
            string mimeType = Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(path.ToLower()));

            if (mimeType == null)
            {
                mimeType = "*/*";
            }

            if (path.StartsWith("content://"))
            {
                uri = Android.Net.Uri.Parse(path);

                DocumentFile pickedDir = DocumentFile.FromSingleUri(Android.App.Application.Context, uri);
                if (pickedDir.Exists())
                {
                    pickedDir.Delete();
                }
            }
            else
            {
                var file = new Java.IO.File(Path.Combine(Android.App.Application.Context.FilesDir.Path, path));
                file.Delete();
                //  uri = FileProvider.GetUriForFile(Android.App.Application.Context, auth, file);
            }
        }
Exemplo n.º 4
0
            /// <summary>
            /// löscht das Objekt
            /// </summary>
            /// <param name="storagename">z.B. "primary" oder "19F4-0903"</param>
            /// <param name="volpath">abs. Pfad im Volume</param>
            /// <returns></returns>
            public bool Delete(string storagename, string volpath)
            {
                DocumentFile doc = GetExistingDocumentFile(storagename, volpath);
                bool         ok  = doc != null && doc.Delete();

                System.Diagnostics.Debug.WriteLine("AndroidDelete(" + storagename + ", " + volpath + ") = " + ok.ToString());

                return(ok);
            }
Exemplo n.º 5
0
 public async Task DeleteAsync(CancellationToken cancellationToken = default(CancellationToken))
 {
     file.Delete();
 }
Exemplo n.º 6
0
        public Task <bool> SaveFileInFolder(FileData fileToSave, FolderData folder, bool shouldOverWrite)
        {
            var uniqueId = Guid.NewGuid();
            var next     = new TaskCompletionSource <bool>(uniqueId);

            // Interlocked.CompareExchange(ref object location1, object value, object comparand)
            // Compare location1 with comparand.
            // If equal replace location1 by value.
            // Returns the original value of location1.
            // ---
            // In this context, tcs is compared to null, if equal tcs is replaced by next,
            // and original tcs is returned.
            // We then compare original tcs with null, if not null it means that a task was
            // already started.
            if (Interlocked.CompareExchange(ref tcs_bool_as_int, next, null) != null)
            {
                return(Task.FromResult <bool>(false));
            }

            EventHandler <PermissionRequestEventArgs> handler = null;

            weakContext.TryGetTarget(out Context newContext);

            var requestPermissionIntent = new Intent(newContext, typeof(RequestPermissionActivity));

            requestPermissionIntent.SetFlags(ActivityFlags.NewTask);
            requestPermissionIntent.PutExtra(RequestPermissionActivity.RequestedPermission, Manifest.Permission.WriteExternalStorage);



            handler = (sender, e) =>
            {
                // Interlocaked.Exchange(ref object location1, object value)
                // Sets an object to a specified value and returns a reference to the original object.
                // ---
                // In this context, sets tcs to null and returns it.
                var task = Interlocked.Exchange(ref tcs_bool_as_int, null);

                RequestPermissionActivity.OnPermissionGranted -= handler;

                if (e.success)
                {
                    try
                    {
                        var test = Android.Net.Uri.Parse(folder.FolderPath);// + "/test.pdf");



                        var documentFile = DocumentFile.FromTreeUri(newContext, test);

                        DocumentFile newFile = documentFile.FindFile(fileToSave.FileName);



                        if (newFile != null)
                        {
                            if (shouldOverWrite)
                            {
                                newFile.Delete();
                            }
                            else
                            {
                                //supposed to create uniuqe name in this case
                                var purefilename  = Path.GetFileNameWithoutExtension(fileToSave.FileName);
                                var fileextention = Path.GetExtension(fileToSave.FileName);
                                fileToSave.FileName = purefilename + "_" + Path.GetRandomFileName() + fileextention;
                            }
                        }

                        documentFile.CreateFile("*/*", fileToSave.FileName);
                        //var documentFile = DocumentFile.FromTreeUri(activity, test);


                        var outputstream = newContext.ContentResolver.OpenOutputStream(newFile.Uri);

                        fileToSave.GetStream().CopyTo(outputstream);

                        outputstream.Flush();
                        outputstream.Close();

                        task.SetResult(e.success);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                        task.SetCanceled();
                    }
                }
            };
            RequestPermissionActivity.OnPermissionGranted += handler;

            //proably need a try statement here
            newContext.StartActivity(requestPermissionIntent);



            return(tcs_bool_as_int.Task);
        }