Screen capture/screenshot button in Xamarin

Asked

Viewed 207 times

0

I am developing an App and I need it to take a screenshot, but not by the standard procedure (home button + power), but by a button inserted on the screen. The button has already been created and is on the screen of the App, someone has some idea of how to make this button make a screenshot?

private void Button_Clicked(object sender, System.EventArgs e)
{
  //procedimento        
}

1 answer

1

The procedure of taking a screen print, consists of drawing an image file with the content displayed on the screen.

Assuming you also want to save the image generated on the SD card for example, the solution would be like this:

Add permission to write to the card in the manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Add the code below within your button click function:

    private void Button_Clicked(object sender, System.EventArgs e)
{   
       // String que recebe o endereço da pasta onde será salva o arquivo
       string path = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, "PrintDirectory");

       // Busca a view principal de sua aplicação
       View v1 = Window.DecorView.RootView;
        v1.DrawingCacheEnabled=true;

        // Cria uma cache para escrever a imagem do tipo bitmap
        Android.Graphics.Bitmap bitmap = Android.Graphics.Bitmap.CreateBitmap(v1.GetDrawingCache(true));

        // Instancia o arquivo na pasta de destino. Neste caso o nome do arquivo será de acordo com o horário, pois garante que não terá arquivos com nomes iguais
        Java.IO.File imageFile = new Java.IO.File(path, System.Environment.TickCount + ".jpg");

        //  Cria um fluxo de bytes para escrever os bytes capturados da tela         
        System.IO.MemoryStream bytes = new System.IO.MemoryStream();
        int quality = 100;


        // Verifica se a pasta destino existe, caso não exista, cria a pasta no cartão SD
        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
        if (!dir.Exists)
            dir.CreateSubdirectory(path);


        // Cria um fluxo de escrita para o arquivo destino
        Java.IO.FileOutputStream fo;
        imageFile.CreateNewFile();
        fo = new Java.IO.FileOutputStream(imageFile);

        //Converte o bitmap para JPEG
        bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Jpeg, quality, bytes);

        // Escreve o arquivo com o print na pasta e depois o fecha-o
        fo.Write(bytes.ToArray());
        fo.Close();
}

A file of type . JPEG will be generated in the chosen folder

  • the part searching for the main view is giving error "An object refrain is required for the non-static "Window.DecorView.Rootview" field, method or property .

  • Are you calling this function directly from Activity ? : Window.Decor.View by: findViewById(android.R.id.content).getRootView(); This type of code may change depending on the Android version.

  • yes, in the same way that was presented in the reply

  • it is worth noting that I am starting in object-oriented programming/ Xamarin

  • I am not succeeding, the problem seems to be in Decorview (speculating)

  • Got the same error? A line android.R.id.content assumes that this is the default name of Activity, if it is not, you will need to adapt.

  • what do you mean by Activity? when you mentioned it before, I thought I was talking about button_clicked

  • Activity in a less technical language is each screen of your application. This property getRootView search for the screen root element(Activity) you are currently using. The button click event has to be inside the Activity in which you want to print, for example Mainactivity. the line android.R.id.content search for the default name but can sometimes have another name as for example: android.R.id.main_layout. You can check the name in the xml file of your Activity

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.