How to share an image (ImageView) using Intent?

1

I'm having a hard time sharing an image from an ImageView from a button, tried several methods, but none worked.

Here is an example of what I want to share: link

I have an array with the path of the images 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 do not know if the problem is with my device that is with Cyanogen, because I already saw several solutions to this problem in Stackoverflow, but none worked.

    
asked by anonymous 10.05.2017 / 21:08

1 answer

1

You need to save the image so you can share:

Come on:

  
  • Permission:
  •   

    In your AndroidManifest.xml file add the permission so that we can save the image to the device:

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

    From 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();
        }
    }
    
      
  • 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"));
        }
    
        
    10.05.2017 / 22:10