How can I share an Image by Mail, Bluetooth, etc?

1

I have a ImageView that has a drawable that was edited while using the app, wanted to take this final version of the edition and send it via E-mail, Bluetooth, etc.

The way I'm doing it, when I send it by email for example, I get information that the image size is 0 bytes, that is, I can not make submissions.

I'm trying this way:

  drawingView.setDrawingCacheEnabled(true);
  Bitmap bitmapp = drawingView.getDrawingCache();
  resultView.setImageBitmap(bitmapp); //Aqui está a imagem que quero enviar
  Bitmap bitmap = bitmapp;
  bitmap = scaleDownBitmap(drawableToBitmap(resultView.getDrawable()), 100, resultView.getContext());// estou trabalhando com o bitmap da resultView

  Bundle param = new Bundle();
  param.putParcelable("BITMAP", bitmap);

  Intent share = new Intent(Intent.ACTION_SEND);
  share.setType("image/png");
  share.putExtras(param); //Estou add ele em uma Intent
  share.putExtra(Intent.EXTRA_STREAM, Uri.parse(saveImage().getAbsolutePath())); 

  startActivity(Intent.createChooser(share, "Share Image"));

saveImage method:

public File saveImage() {

    int imageNum = 0;
    File imagesFolder = new File(Environment.getExternalStorageDirectory()+File.separator+"DCIM", "@string/app_name");
    imagesFolder.mkdirs();
    String fileName = "imageKL_" + String.valueOf(imageNum) + ".jpg"; //ALTERAR PARA PNG ! ! !
    File output = new File(imagesFolder, fileName);
    while (output.exists()){
        imageNum++;
        fileName = "imageKL_" + String.valueOf(imageNum) + ".jpg"; //ALTERAR PARA PNG ! ! !
        output = new File(imagesFolder, fileName);
    }
    try {
        drawingView.setDrawingCacheEnabled(true);
        Bitmap bitmapp = drawingView.getDrawingCache();
        resultView.setImageBitmap(bitmapp);
        Bitmap bitmap = bitmapp;

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
        FileOutputStream fo = new FileOutputStream(output);
        fo.write(bytes.toByteArray());
        fo.flush();
        fo.close();
        MediaScannerConnection.scanFile(EditImage.this, new String[]{output.getAbsolutePath()}, null, null);

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return output;
}

I appreciate the attention and if anyone knows or has any idea how to solve this problem, every tip is welcome!

    
asked by anonymous 17.12.2015 / 14:58

2 answers

1

Well, I took a look at this site, and found it a pretty cool way to do it. : D Anyway, thanks for the attention and help!

Website: link

Code:

        final Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("image/jpg");
        //final File photoFile = new File(getFilesDir(), "foo.jpg");
        final File photoFile = saveImage(); //Substitui a linha de cima por esta. . .
        shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
        startActivity(Intent.createChooser(shareIntent,"Share image using"));//"Share image using"

The saveImage () method appears in the question:)

Hugs!

    
17.12.2015 / 19:03
3

The problem is that you are invoking setDrawingCacheEnabled() but you are not building your cached image. Try something similar:

imageView.setDrawingCacheEnabled(true);
imageView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
imageView.layout(0, 0, imageView.getMeasuredWidth(), imageView.getMeasuredHeight());
imageView.buildDrawingCache(true);

Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache());
imageView.setDrawingCacheEnabled(false);
    
17.12.2015 / 15:55