将图像复制到另一个文件夹
Copy image to another folder
我正在使用 Xamaring 表单,我正在尝试将选定的图像路径复制到智能手机上的另一个位置,但无法正常工作。
知道为什么以及如何解决它吗?
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);
}
}
}
在我的清单中我获得了权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
错误信息:
"/data/user/0/com.companyname.WightLoss/files/WightLossPersonal 是一个目录"
你的主要问题是你没有在新目录中传递文件名。
所以这就像你试图复制目录本身,而不是文件!
基本上你必须将文件名与目录结合起来
然后将其传递给 Copy() 方法。
string destFolder = Path.Combine(dirToCreate, file.Name);
System.IO.File.Copy(file.FilePath, destFolder , true);
但让我们让代码更简洁。我会评论代码以便更好地理解。
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);
//}
}
}
我正在使用 Xamaring 表单,我正在尝试将选定的图像路径复制到智能手机上的另一个位置,但无法正常工作。 知道为什么以及如何解决它吗?
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);
}
}
}
在我的清单中我获得了权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
错误信息:
"/data/user/0/com.companyname.WightLoss/files/WightLossPersonal 是一个目录"
你的主要问题是你没有在新目录中传递文件名。 所以这就像你试图复制目录本身,而不是文件!
基本上你必须将文件名与目录结合起来 然后将其传递给 Copy() 方法。
string destFolder = Path.Combine(dirToCreate, file.Name);
System.IO.File.Copy(file.FilePath, destFolder , true);
但让我们让代码更简洁。我会评论代码以便更好地理解。
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);
//}
}
}