1

I currently have an app in which I'm taking photos and I'm trying to share them directly through the app. The problem I'm having is that when the chooser comes up and I pick one, I get an error toast that pops up and says "Unable to attach file". Then it just takes me back into the app and everything else still works. I'm assuming that this means that I have to save the image first, but I'm not sure if I'm doing that correctly. I found some examples online of how to save the images but I'm not sure if that's the problem or if the share intent is the problem.

    final Button shareButton = (Button) findViewById(R.id.shareButton);
    shareButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            imageView.setDrawingCacheEnabled(true);
            Bitmap b = imageView.getDrawingCache();
            MediaStore.Images.Media.insertImage(getContentResolver(), b, "title", "description");
            Intent shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.setType("image/jpeg");
            BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
            Bitmap image =  bitmapDrawable.getBitmap();
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            image.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temp.jpg");
            try {
                f.createNewFile();
                FileOutputStream fo = new FileOutputStream(f);
                fo.write(bytes.toByteArray());
            } catch (IOException e) {
                e.printStackTrace();
            }
            shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().getPath()));
            startActivity(Intent.createChooser(shareIntent, "Share Image"));


        }
    });

I have "title" and "description" in as placeholders until I can get this to work.

Stephen Burns
  • 162
  • 2
  • 17
  • 1
    Add the error in your question. – Pipiks Jul 28 '17 at 19:47
  • @Pipiks I'm not getting an error in android studio or anything, my app isn't crashing. A toast just pops up saying "unable to attach file" and then it returns me to the app home screen, which still works normally. I added that bit to the post. – Stephen Burns Jul 28 '17 at 19:51
  • Ok, I think the wrong line is `shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().getPath()));` you need to set the image path in the second parameter – Pipiks Jul 28 '17 at 19:53
  • @Pipiks What is that file path going to be? Do I just set a directory that's in the external storage? – Stephen Burns Jul 28 '17 at 19:57

1 Answers1

1

Try to replace :

shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().getPath(‌​)));

With :

shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));

This question can help you.

Pipiks
  • 2,018
  • 12
  • 27
  • Yeah I actually have that example open in another tab while I'm doing this, trying to see where mine could be breaking in comparison to that. This fix worked for me. Thanks! – Stephen Burns Jul 28 '17 at 20:02