![](/img/trans.png)
[英]How to ask authorization for save image in Gallery on Android using Xamarin?
[英]how to save photo in gallery folder in android using xamarin
如何將照片保存在圖庫文件夾中? 我正在使用 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 方法將其保存到系統相冊中。
在 forms 項目中創建一個接口:
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.