SimonSimCity / Xamarin-CrossDownloadManager

A cross platform download manager for Xamarin
MIT License
149 stars 68 forks source link

Sending email with downloaded file #100

Closed csampaio26 closed 5 years ago

csampaio26 commented 5 years ago

Steps to reproduce

  1. Setting the path for downloaded files

    CrossDownloadManager.Current.PathNameForDownloadedFile = new System.Func<IDownloadFile, string>(file => {
                string fileName = Android.Net.Uri.Parse(file.Url).Path.Split('/').Last();
                return System.IO.Path.Combine(ApplicationContext.GetExternalFilesDir(Environment.DirectoryDownloads).AbsolutePath, fileName);
            });
  2. Accessing the file to send it as an attachment

    
    string fileName = Android.Net.Uri.Parse(url).Path.Split('/').Last();

string path = Path.Combine(Android.App.Application.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryDownloads).AbsolutePath, fileName);

            email.PutExtra(Intent.ExtraStream, Android.Net.Uri.FromFile(new Java.IO.File(path)));

            email.SetType("application/pdf");
        }

        Forms.Context.StartActivity(Intent.CreateChooser(email, "Send Email"));


### Expected behavior
It should send the document as an email attachment

### Actual behavior
Instead, it tells that the document has 0 bytes and the receiver doesnt get the document. 

### Configuration

**Platform:** 
- [ ] iOS
- [x] Android
- [ ] UWP
- [ ] Other

**Device:** 
- [ ] Simulator
- [x] Real device (Huawei PSmart)
SimonSimCity commented 5 years ago

Can you share a full example? I e.g. don't know when you're trying to send it via mail.

The best for me would be if you could fork this repo and adjust the sample so you get the behavior you're facing.

csampaio26 commented 5 years ago

I was using the wrong way to download files. I should be doing:

`

                        bool isDownloading = true;
                        CrossDownloadManager.Current.Start(File);

                        File.PropertyChanged += async (sender, e2) =>
                        {
                            if (File.Status == DownloadFileStatus.COMPLETED)
                            {
                                await Task.Delay(100);
                                isDownloading = IsDownloading(File);
                            }
                        };

`

And to attach to the email:


                string fileName = Android.Net.Uri.Parse(url).Path.Split('/').Last();
                string path = Path.Combine(Android.App.Application.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryDownloads).AbsolutePath, fileName);

                var file = new Java.IO.File(path);
                var uri = Android.Net.Uri.FromFile(file);

                email.PutExtra(Intent.ExtraStream, uri);