Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Taking screen shot of a SurfaceView in android

Tags:

android

I am using following method to taking screen shot of a particular view which is a SurfaceView.

public void takeScreenShot(View surface_view){

    // create bitmap screen capture
    Bitmap bitmap;
    View v1 = surface_view;
    v1.setDrawingCacheEnabled(true);
    bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0, bos);
    byte[] imageData = bos.toByteArray();

}

the problem is its giving me the whole activity screen image. But I need to take screen shot of the particular view. I tried other ways but those give me a black screen as screen shot, some posts says that it requires rooted device. Can any one help me please. I'm in need of this solution. Help me....

like image 742
Mostafa Imran Avatar asked Oct 04 '12 09:10

Mostafa Imran


People also ask

How do you take a screenshot of a specific section on Android?

Hold down the power button and press the volume-down button. Or... Go to the app and / or page you want to take a screenshot of. Swipe up from the bottom of the screen to the Recents screen; you'll see a button for Screenshot at the bottom of the screen.

How do I take a screenshot of a long Web page on Android?

Take a scrolling screenshot on your Android To take a scrolling screenshot on Android, navigate to the content screen you'd like to save. Press the power and volume down buttons at the same time to trigger the screenshot tool.

How can I take screenshot without pressing button in Android?

As long as you're running Android 12, you can use a unique swipe gesture to take a screenshot on the Pixel 6. All you need to do is swipe up from the bottom of the screen and hold your finger on the screen for a second. This will open your app drawer, where all your recently opened apps appear.


1 Answers

Surface view is a view but item on surface view like bitmap or other object are not any view. So while you capture surface view it will capture every thing on the surface. You have to use other view like image view or other above the surface view and then capture those view.

First get the view whose picture want to take then do this

            Bitmap bitmap;
            View rv = **your view**
            rv.setDrawingCacheEnabled(true);
            bitmap = Bitmap.createBitmap(rv.getDrawingCache());
            rv.setDrawingCacheEnabled(false);

            // Write File to internal Storage

            String FILENAME = "captured.png";
            FileOutputStream fos = null;

            try {

                fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);

            } catch (FileNotFoundException e1) {

                e1.printStackTrace();
                Log.v("","FileNotFoundException: "+e1.getMessage());

            }

            try {
                bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
                fos.flush();
                fos.close();

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
like image 170
Chinmoy Debnath Avatar answered Nov 23 '22 20:11

Chinmoy Debnath