1

I need to take a screen shot and save the screen shot. I need to do this without using any connection to PC or without unrooting the phone. I need to do this whenever a event is triggered . For example when an ad is shown in a game ... or when the game ends and shows the scores in snake etc. Can you please let me know How Can i do this. I saw some tutorilas and they gave the code but that doesnt seem to work

private void getScreen()
   {
    View content = findViewById(R.id.layoutRoot);
    Bitmap bitmap = content.getDrawingCache();
    File file = new File("/sdcard/test.png");
    try 
    {
        file.createNewFile();
        FileOutputStream ostream = new FileOutputStream(file);
        bitmap.compress(CompressFormat.PNG, 100, ostream);
        ostream.close();
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}
kannappan
  • 2,250
  • 3
  • 25
  • 35
Vaali
  • 151
  • 2
  • 11
  • possible duplicate http://stackoverflow.com/questions/5929403/take-screenshot-of-android-screen-and-save-to-sd-card and http://stackoverflow.com/questions/5939987/android-take-screenshot-via-code – DynamicMind Jun 15 '11 at 07:18

3 Answers3

1
View ve = findViewById(R.id.loyout);
        ve.setDrawingCacheEnabled(true);
        Bitmap b = ve.getDrawingCache();

        String extr = Environment.getExternalStorageDirectory().toString()
                + "/SaveCapture";
        myPath = new File(extr);

        if (!myPath.exists()) {
            boolean result = myPath.mkdir();
            Toast.makeText(this, result + "", Toast.LENGTH_SHORT).show();
        }
        myPath = new File(extr, getString(R.string.app_name) + ".jpg");

        Toast.makeText(this, myPath.toString(), Toast.LENGTH_SHORT).show();
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(myPath);

            b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
            MediaStore.Images.Media.insertImage(getContentResolver(), b,
                    "Screen", "screen");


        }

        catch (FileNotFoundException e) {

            Log.e("Error", e + "");
        }

        catch (Exception e) {

            Log.e("Error", e + "");
        }
Arun PS
  • 4,610
  • 6
  • 41
  • 56
1

You have to enable the cache first, before calling getDrawingCache().

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
levis501
  • 4,117
  • 23
  • 25
0

Can you give more information as to what does not work when you run that code ? Does it not capture what you want ? Does it crash ?

Make sure you change the R.id.layoutroot correctly with your root layout... Beside that it seems like it would work...

<com.example.android.snake.SnakeView
 android:id="@+id/snake"
    android:layout_width="match_parent"
            android:layout_height="match_parent"
            tileSize="24"
            />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
     android:id="@+id/text"
        android:text="@string/snake_layout_text_text"
        android:visibility="visible"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center_horizontal"
        android:textColor="#ff8888ff"
        android:textSize="24sp"/>
</RelativeLayout>

Edit...

So for example, if you use that layout you just put there, you should change the R.id.layout into R.id.snake, that's because of this line : android:id="@+id/snake".

I don't think there is an easy way to find /get the id of the "root" layout of a view (if you wanted to take screenshot of anything the phone is showing.

I just checked the launcher and another application, seems like most app are placed into a FrameLayout with id/content, so you can try to use android.R.id.content, but there is no guaranty this will work every time...

Matthieu
  • 16,103
  • 10
  • 59
  • 86
  • It says that it cannot resolve R.id.layoutroot . So can you let me know how can I map it to the correct layoutroot. – Vaali Jun 15 '11 at 13:55
  • Can you post your main.xml or the layout file you are using for the view you want to take a screenshot of ? At least one of them... – Matthieu Jun 15 '11 at 14:11
  • I donot know much about this .... I pasted the snakelayout.xml. Is this the one you are talking about or is there another file. – Vaali Jun 15 '11 at 14:28
  • So if i want to take a screen shot of any application , I need to get the root layout which is not the same for all applications... and I tried content too ... its giving the same error. – Vaali Jun 15 '11 at 16:07
  • @ Matthieu Thank you it worked . I also added this statement content.setDrawingCacheEnabled(true); and the cnage you told. – Vaali Jun 15 '11 at 21:57