marios
marios

Reputation: 153

Copy image to another folder

I am using Xamaring forms and I am trying to copy selected image path to another location on smartphone but is not working. Any ideas why and how to fix it?

  private async Task btn_AddImg_ClickedAsync(object sender, EventArgs e)
        {
            var file = await CrossFilePicker.Current.PickFileAsync();
            if (file != null)
            {
                Error.IsVisible = true;
                Error.Text = file.FilePath;

                var dirToCreate = Path.Combine(Android.App.Application.Context.FilesDir.AbsolutePath, "WightLossPersonal");
                if (!Directory.Exists(dirToCreate))
                {

                    var x= Directory.CreateDirectory(dirToCreate);
                    System.IO.File.Copy(file.FilePath, dirToCreate, true);

                }
                else
                {
                   System.IO.File.Copy(file.FilePath, dirToCreate, true);
                }

            }
        }

In my Manifest I got permissions

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Error Message :

"/data/user/0/com.companyname.WightLoss/files/WightLossPersonal is a directory"

enter image description here

Upvotes: 1

Views: 2174

Answers (1)

Kaj
Kaj

Reputation: 814

Well your main issue is you didn't pass the file name in the new dir. So it's like you are trying to copy the dir itself, not the file !

Basically you have to combine the file name with the dir then pass it to Copy() method.

string destFolder = Path.Combine(dirToCreate, file.Name);
System.IO.File.Copy(file.FilePath, destFolder , true);

but let's make the code more clean. And I'll comment the code for better understanding.

private async Task btn_AddImg_ClickedAsync(object sender, EventArgs e)
        {
            var file = await CrossFilePicker.Current.PickFileAsync();
            if (file != null)
            {
                Error.IsVisible = true;
                Error.Text = file.FilePath;

                var dirToCreate = Path.Combine(Android.App.Application.Context.FilesDir.AbsolutePath, "WightLossPersonal");
                if (!Directory.Exists(dirToCreate))
                {
                      Directory.CreateDirectory(dirToCreate);
                   // var x= Directory.CreateDirectory(dirToCreate); // don't need that variable x here since you don't want to use it later
                    //System.IO.File.Copy(file.FilePath, dirToCreate, true); No need here, will copy it in all ways down .

                }
                //else   // you don't need else, copy the file when finishing the check.
                //{

                  // Make a new path to compine the dir and the fileName
                   string destFolder = Path.Combine(dirToCreate, file.Name);
                   System.IO.File.Copy(file.FilePath, destFolder , true);
                //}

            }


  }

Upvotes: 1

Related Questions