Forum Xamarin.Forms

Convert GoogleMap Snapshot Java solution to Xamarin C#


  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited October 2019


    I convert it for you, but in his reply he did not achieve the openFileOutput ,you need achieve it by yourself. If you have convert issue, you need read this article at the first(That is a basic and important skill when you use xamarin forms).

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using Android.App;
    using Android.Content;
    using Android.Gms.Maps;
    using Android.Graphics;
    using Android.OS;
    using Android.Provider;
    using Android.Runtime; 
    using Android.Util;
    using Android.Views;
    using Android.Widget;
    using Java.IO;
    using File = Java.IO.File;
    class Snapshot : Java.Lang.Object, GoogleMap.ISnapshotReadyCallback
        Context mContext;
        public Snapshot( Context context)
            mContext = context;
        private void openShareImageDialog(string filePath)
            File file = mContext.GetFileStreamPath(filePath);
            if (!filePath.Equals(""))
                 ContentValues values = new ContentValues(2);
                values.Put(MediaStore.Images.ImageColumns.MimeType, "image/jpeg");
                values.Put(MediaStore.Images.ImageColumns.Data, file.AbsolutePath );
                 var contentUriFile = mContext.ContentResolver.Insert(MediaStore.Images.Media.ExternalContentUri, values);
                 Intent intent = new Intent(Android.Content.Intent.ActionSend);
                intent.PutExtra(Android.Content.Intent.ExtraStream, contentUriFile);
                mContext.StartActivity(Intent.CreateChooser(intent, "Share Image"));
                //This is a custom class I use to show dialogs...simply replace this with whatever you want to show an error message, Toast, etc.
              //  DialogUtilities.showOkDialogWithText(this, R.string.shareImageFailed);
        private Stream openFileOutput(string filePath, object mODE_WORLD_READABLE)
            //you need to achieve that
            Stream outputStream = new Stream();
            return outputStream;
        public void OnSnapshotReady(Bitmap snapshot)
            Bitmap bitmap = snapshot;
            OutputStream fout = null;
            String filePath = System.Environment.TickCount + ".jpeg";
                fout = openFileOutput(filePath,
                // Write the string to the file
                bitmap.Compress(Bitmap.CompressFormat.Jpeg, 90, fout);
            catch (Java.IO.FileNotFoundException e)
                // TODO Auto-generated catch block
                Log.Debug("ImageCapture", "FileNotFoundException");
                Log.Debug("ImageCapture", e.Message );
                filePath = "";
            catch (Java.IO.IOException e)
                // TODO Auto-generated catch block
                Log.Debug("ImageCapture", "IOException");
                Log.Debug("ImageCapture", e.Message);
                filePath = "";
  • GolfMan2000GolfMan2000 Member ✭✭

    very impressive and informative!

    I worked on it so here are some questions

    should this be implemented outside of my activity like

    namespace Location.Droid
    public class Snapshot : Java.Lang.Object, GoogleMap.ISnapshotReadyCallback
    …. your great Java converted solution

    [Activity(Label = "Cookies", MainLauncher = true, NoHistory = false, LaunchMode = LaunchMode.SingleTop,
        ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.ScreenLayout)]
    public class MainActivity : AppCompatActivity, IOnMapReadyCallback
      …. My current working GoogleMap Fragment


    ***Could it be used from OnBackPressed() which is confusing since there is a built-in Snapshot command

    MyMap.Snapshot(Snapshot); //There is GoogleMap Snapshot and our class called Snapshot

    ***Also can you please show me how I can replace all the dance work around openFileOutput with

           var filename = System.IO.Path.Combine(Environment.GetExternalStoragePublicDirectory
                    (Environment.DirectoryPictures).ToString(), "NewFolder");
            filename = System.IO.Path.Combine(filename, "filename.jpg");
            using (var fileOutputStream = new FileOutputStream(filename))
                await fileOutputStream.WriteAsync(screenshotData);      //screenshotData is from Google Snapshot as byte[]

    Thanks in advance

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    If you use it outside Activity, if you need the current Activity, you can use this plugin

    But If you want achieve the screenshot function. You can use this

  • GolfMan2000GolfMan2000 Member ✭✭
    If you use it outside Activity, if you need the current Activity, you can use this plugin...I am not sure what that means.

    It's amazing when you do research on the search engines how much information comes up and of course that plug-in came up and I've already tried it before but it only captures the form not the map fragment content. There is quite a bit written about why that is... But most of those are talking heads that are ill informed.

    My Xamarin solution seemed to be stable and working well and it shows the map fragment and everything is functional about the maps. I used the form button event to trigger the plug-in capture. I save it to local storage and I can see the file and I can open up the jpeg only that is black. I'm not sure where I'm going wrong in plug-in implementation.
  • LeonLuLeonLu Member, Xamarin Team Xamurai

    If you did not get the helpful answer, you can open a free support.
    Free support for all Xamarin Developers

Sign In or Register to comment.