0

I am trying to take a screenshot using Floating Widget. But I can't find any way of doing so. I searched for MediaProjection API but couldn't find anything helpful. Right now, If I tap the floating widget, it only captures the screenshot of the floating widget.

 captureButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Screenshot.java is my class for taking the screenshot
            Bitmap bitmap = Screenshot.takescreenshotOfView(v);
            OutputStream fOut = null;
            Uri outputFileUri;
            try {
                File root = new File(Environment.getExternalStorageDirectory()
                        + File.separator + "DUOProfile" + File.separator);
                root.mkdirs();
                File sdImageMainDirectory = new File(root, "profilepic"+".jpg");
                Toast.makeText(getApplicationContext(), "Picture saved in DUOProfile folder",Toast.LENGTH_SHORT).show();
                outputFileUri = Uri.fromFile(sdImageMainDirectory);
                fOut = new FileOutputStream(sdImageMainDirectory);
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Error occured. Please try again later.",Toast.LENGTH_SHORT).show();
            }
            try {
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                fOut.flush();
                fOut.close();
            } catch (Exception e) {
            }
        }
    });

Screenshot.java

package com;

import android.graphics.Bitmap;
import android.view.View;

public class Screenshot  {

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

    public static Bitmap takescreenshotOfView(View view) {
        return takescreenshot(view.getRootView());
    }

}
Chirag
  • 555
  • 1
  • 5
  • 20
Waheed Abbas
  • 187
  • 1
  • 4
  • 18

1 Answers1

-2

Pass the view in takescreenshotOfView method

Bitmap bitmap = takescreenshotOfView(view);

This will return bitmap image, just saved in your internal/sd card

 public Bitmap takescreenshotOfView(View view) {
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
                view.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
        return bitmap;
    }
Android Guy
  • 573
  • 1
  • 8
  • 18