Search code examples
androidbackgroundscreenshot

How I use the image of a screenshot?


I want to use the code of this link: How to take a screenshot and share it programmatically to take screenshots of my application. But I want the picture which is produced to use it to change my background, like this Layout.setBackgroundResource(resid). How I can do this? Where I will find the image's path to use it to change Background?


Solution

  • First you need the view you want a screen shot of:

    View anyView = findViewById(R.id.anyView);
    

    Then use this method to take a screenshot:

    public Bitmap screenShot(View view) {
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
                view.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
        return bitmap;
    }
    

    like this:

    Bitmap screenshot = screenShot(anyView);
    

    Bitmaps can't be set as a background image so lets convert it to a drawable:

    Drawable drawableScreenshot = new BitmapDrawable(getResources(), screenshot);
    

    Now set it to be the View background:

    anyView.setBackgroundDrawable(drawableScreenshot);