Issue
I want to pass an SKImage and a string filename to save the picture to phone's directory.
The method what I am trying to call:
namespace LevelMeter.Droid
{
public class SaveImage_Droid : SaveImage
{
public SaveImage_Droid(SKImage snapshot, string fileName)
{
SaveImage(snapshot, fileName);
}
public void SaveImage(SKImage snapshot, string fileName)
{
var pictures = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).AbsolutePath;
Bitmap bm = snapshot.ToBitmap();
var filePath = System.IO.Path.Combine(pictures, fileName);
var stream = new FileStream(filePath, FileMode.Create);
bm.Compress(Bitmap.CompressFormat.Jpeg, 90, stream);
stream.Flush();
stream.Close();
}
}
}
I made this interface:
namespace LevelMeter
{
public interface SaveImage
{
void SaveImage(SKImage snapshot, string fileName);
}
}
And I want to call it from
namespace LevelMeter.Views
{
public void SaveButton_Clicked(object sender, EventArgs e)
{
SKImage snapshot = surface.Snapshot();
string fileName = System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
SaveImage(snapshot, fileName); //I cant't reach it
}
}
Solution
You can use DependencyService to call method from shared project to .Android
project:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
public void SaveButton_Clicked(object sender, EventArgs e)
{
SKImage snapshot = surface.Snapshot();
string fileName = System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
DependencyService.Get<ISaveImage>().SaveImage(snapshot, fileName);
}
}
public interface ISaveImage
{
void SaveImage(SKImage snapshot, string fileName);
}
And in Android project, register it: Registration by attribute
[assembly: Dependency(typeof(SaveImage_Droid))]
namespace LevelMeter.Droid
{
public class SaveImage_Droid : SaveImage
{
public SaveImage_Droid(SKImage snapshot, string fileName)
{
SaveImage(snapshot, fileName);
}
public void SaveImage(SKImage snapshot, string fileName)
{
var pictures = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).AbsolutePath;
Bitmap bm = snapshot.ToBitmap();
var filePath = System.IO.Path.Combine(pictures, fileName);
var stream = new FileStream(filePath, FileMode.Create);
bm.Compress(Bitmap.CompressFormat.Jpeg, 90, stream);
stream.Flush();
stream.Close();
}
}
}
Answered By - nevermore
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.