How can I share an image via Mail, Bluetooth, etc?

Asked

Viewed 75 times

1

I own a ImageView that has a drawable that was edited during the use of the app, wanted to take this final edition and send it by Email, Bluetooth, etc..

The way I’m doing, when I send by email for example, I get information that the image size is 0 bytes, I mean, I can’t send..

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"));

Metodo saveImage:

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 in case anyone knows or has any idea how to solve this problem, any tip is welcome!

  • Hello, now include the method in the question. Hug!

2 answers

3

The problem is that you are invoking the setDrawingCacheEnabled() but you’re not caching your 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);
  • Hello, thanks for the tip, I’m trying here, if you succeed or will not warn soon. Hug!

1


Guys, I took a look at this site, and I found a really cool way to do it. D Anyway, thanks for your attention and help!

Website: https://github.com/codepath/android_guides/wiki/Sharing-Content-with-Intents

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 :)

Hug!

Browser other questions tagged

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