问题描述
如何将照片保存在图库文件夹中?我正在使用 ImageCropper.Forms.Fix.v2 和 Xam.Media.Plugin
代码工作正常,但保存在位置:/data/user/0/com.companyname.projectname/cache/cropped3243.png
我想把它保存在画廊文件夹中。我该怎么做?
protected async void TapGestureRecognizerCommand(object sender,EventArgs e)
{
await CrossMedia.Current.Initialize();
ImageSource TempimageFile = null;
new ImageCropper()
{
PageTitle = "Crop Photo",AspectRatioX = 3,//
CropShape = ImageCropper.CropShapeType.Rectangle,//Cropt shape
SelectSourceTitle = "Select source",// Pop up Title
TakePhotoTitle = "Take Photo",// Popup - 1st option
PhotoLibraryTitle = "Photo Library",//Popup - 2nd option
Success = (imageFile) =>
{
Device.BeginInvokeOnMainThread(() =>
{
TempimageFile = ImageSource.FromFile(imageFile);
displayAlert("test","te: " + TempimageFile,"OK");
ImageURL.source = TempimageFile;
});
}
}.Show(this);
}
解决方法
例如,您可以使用 DependencyService 调用 natvie 方法将其保存到系统相册。
在你的表单项目中创建一个界面:
public interface ISaveToGallery
{
void Save(string filePath,string fileName);
}
然后在您的 Android 项目中:
class SaveToGallery: ISaveToGallery
{
public void Save(string filePath,string fileName)
{
MediaStore.Images.Media.InsertImage(Android.App.Application.Context.ContentResolver,filePath,fileName,null);
}
}
这样称呼:
protected async void TapGestureRecognizerCommand(object sender,EventArgs e)
{
await CrossMedia.Current.Initialize();
ImageSource TempimageFile = null;
new ImageCropper()
{
PageTitle = "Crop Photo",AspectRatioX = 3,//
CropShape = ImageCropper.CropShapeType.Rectangle,//Cropt shape
SelectSourceTitle = "Select source",// Pop up Title
TakePhotoTitle = "Take Photo",// Popup - 1st option
PhotoLibraryTitle = "Photo Library",//Popup - 2nd option
Success = (imageFile) =>
{
Device.BeginInvokeOnMainThread(() =>
{
TempimageFile = ImageSource.FromFile(imageFile);
DisplayAlert("test","te: " + TempimageFile,"OK");
ImageURL.Source = TempimageFile;
DependencyService.Get<ISaveToGallery>().Save(imageFile,"your file name");
});
}
}.Show(this);
}