1

I am trying to take a screenshot programatically and then share it to another app, using Intent. The problem that I face is that the photo is sent as a text file.

The button listener looks like this:

shareButton = rootView.findViewById(R.id.shareButton);
    shareButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Bitmap screenshot = Screenshot.takeScreenshot(view.getRootView());
            String filename = "eduMediaPlayerScreenshot";
            String sharePath = Screenshot.storeScreenshot(screenshot, filename);
            Intent intent = Screenshot.shareScreenshot(sharePath, view.getRootView());
            startActivity(intent);
        }
    });

And the Screenshot class like this:

    public class Screenshot {

    public static Bitmap takeScreenshot(View view) {
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache(true);
        Bitmap screenshot = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false);
        return screenshot;
    }

    public static String storeScreenshot(Bitmap screenshot, String filename) {
        String path = Environment.getExternalStorageDirectory().toString() + "/" + filename;
        OutputStream out = null;
        File imageFile = new File(path);

        try {
            out = new FileOutputStream(imageFile);
            screenshot.compress(Bitmap.CompressFormat.JPEG, 99, out);
            out.flush();

        } catch (FileNotFoundException e) {
            Log.i("Exception:", "File not found.");

        } catch (IOException e) {
            Log.i("Exception:", "Cannot write to output file.");

        } finally {

            try {
                if (out != null) {
                    out.close();
                    return imageFile.toString();
                }

            } catch (Exception e) {
                Log.i("Exception:", "No output file to close.");
            }

        }
        return null;
    }

    public static Intent shareScreenshot(String sharePath, View view) {
        File file = new File(sharePath);
        Uri uri = FileProvider.getUriForFile(view.getContext(),
                BuildConfig.APPLICATION_ID + ".provider", file);
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        return intent;
    }
}

The shared photo looks like this: not-the-best-photo

Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52
poppy22
  • 31
  • 1
  • 5
  • Replace `image/*` with `image/jpeg`. You are the one asking to share the content -- you are the one who has to say what the specific MIME type is. – CommonsWare Dec 30 '18 at 20:09
  • @CommonsWare Thanks, but apparently it doesn't work; it's the same text file sent. – poppy22 Dec 30 '18 at 20:27

1 Answers1

0

As Android will not recognize by default an image without extension, you should add .jpg to the end of the filename in the intent above. Without it, there should be a MIME type specified. More details on that here.

This is the code you might want to use.

shareButton = rootView.findViewById(R.id.shareButton);
shareButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Bitmap screenshot = Screenshot.takeScreenshot(view.getRootView());
        String filename = "eduMediaPlayerScreenshot.jpg";
        String sharePath = Screenshot.storeScreenshot(screenshot, filename);
        Intent intent = Screenshot.shareScreenshot(sharePath, view.getRootView());
        startActivity(intent);
    }
});
vanntile
  • 2,727
  • 4
  • 26
  • 48