How to share an image (Imageview) using Intent?

Asked

Viewed 1,160 times

1

I am having a great difficulty in sharing an image of an Imageview from a button, tried several methods, but none worked.

Here’s an example of what I’m trying to share: http://www.mediafire.com/view/l0b5eobtpdbeoen/Screenshot_20170509-135902.png

I have an array with the image path in Drawable

final int[] photos = {
                R.drawable.abrir_a_boca,
                R.drawable.adicao_de_quartos,
                R.drawable.agarrado_firmemente,
                R.drawable.agradeca,
                R.drawable.alfaiate,
                R.drawable.ancora,
}

compartilhar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                shareImage();
            }

            private void shareImage() {

                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.putExtra(Intent.EXTRA_STREAM, "local do arquivo");
                shareIntent.setType("image/jpeg");
                startActivity(shareIntent);
            }

but it never worked, now I don’t know if the problem is my device that has Cyanogen, because I’ve seen several solutions to this problem in Stackoverflow, but none worked.

1 answer

1


It is necessary to save the image so that it can be shared:

Come on:

  1. Permission:

In his file Androidmanifest.xml add the permission so we can save the image on the device:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  1. Permissions at runtime:

Starting with Android 6.0 (API level 23), users grant permissions to applications while they are running, not when they are installed.

To do this add the following code method:

private static final int SOLICITAR_PERMISSAO = 1;
private void checarPermissao(){

    // Verifica  o estado da permissão de WRITE_EXTERNAL_STORAGE
    int permissionCheck = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);


    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        // Se for diferente de PERMISSION_GRANTED, então vamos exibir a tela padrão 
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, SOLICITAR_PERMISSAO);
    } else {
        // Senão vamos compartilhar a imagem
        sharedImage();
    }
}
  1. Share the image:

Now, let’s save and share the image:

   private void sharedImage(){
        // Vamos carregar a imagem em um bitmap
        Bitmap b = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round);
        Intent share = new Intent(Intent.ACTION_SEND);
        //setamos o tipo da imagem
        share.setType("image/jpeg");
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        // comprimomos a imagem
        b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        // Gravamos a imagem
        String path = MediaStore.Images.Media.insertImage(getContentResolver(), b, "Titulo da Imagem", null);
        // criamos uam Uri com o endereço que a imagem foi salva
        Uri imageUri =  Uri.parse(path);
        // Setmaos a Uri da imagem
        share.putExtra(Intent.EXTRA_STREAM, imageUri);
        // chama o compartilhamento
        startActivity(Intent.createChooser(share, "Selecione"));
    }
  • Thank you for the reply and for the beautiful tutorial, I will already test it.

  • Hello, Thiago, hello. In this line Activitycompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, SOLICITAR_PERMISSAO); what would this REQUEST PERMISSION be? Because you could not solve this parameter. Thank you.

  • Thiago, I don’t know how to thank you. Perfect, it worked smoothly the code. Very good. I’m almost a week stuck with this problem and tested several methods and none had worked. Thank you for your contribution and for sharing your knowledge. Thanks a lot. Hug.

Browser other questions tagged

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