Skip to main content

Android take screen shot programatically



First off i am writing a root app so root permissions are no issue. I've searched and searched and found a lot of code that never worked for me here is what i've pieced together so far and sorta works. When i say sorta i mean it makes an image on my /sdcard/test.png however the file is 0 bytes and obviously can't be viewed.







public class ScreenShot extends Activity{



View content;



@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.blank);

content = findViewById(R.id.blankview);

getScreen();

}



private void getScreen(){

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();

}

}

}







Any help on how i can take a screen shot in android via code would be greatly appreciated thank you!





===EDIT===





The following is everything i'm using the image is made on my sdcard and is no longer 0bytes but the entire thing is black there is nothing on it. I've bound the activity to my search button so when i'm some where on my phone i long press search and it is supposed to take a screen shot but i just get a black image? Everything is set transparent so i'd think it should grab whatever is on the screen but i just keep getting black





Manifest







<activity android:name=".extras.ScreenShot"

android:theme="@android:style/Theme.Translucent.NoTitleBar"

android:noHistory="true" >

<intent-filter>

<action android:name="android.intent.action.SEARCH_LONG_PRESS" />

<category android:name="android.intent.category.DEFAULT" />

</intent-filter>

</activity>







XML







<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#00000000"

android:id="@+id/screenRoot">

</LinearLayout>







Screenshot class







public class ScreenShot extends Activity{



View content;



@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.screenshot);

content = findViewById(R.id.screenRoot);

ViewTreeObserver vto = content.getViewTreeObserver();

vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

@Override

public void onGlobalLayout() {

content.getViewTreeObserver().removeGlobalOnLayoutListener(this);

getScreen();

}

});

}



private void getScreen(){

View view = content;

View v = view.getRootView();

v.setDrawingCacheEnabled(true);

Bitmap b = v.getDrawingCache();

String extr = Environment.getExternalStorageDirectory().toString();

File myPath = new File(extr, "test.jpg");

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) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

finish();

}

}





Source: Tips4all

Comments

  1. Here you go...I used this:

    View v = view.getRootView();
    v.setDrawingCacheEnabled(true);
    Bitmap b = v.getDrawingCache();
    String extr = Environment.getExternalStorageDirectory().toString();
    File myPath = new File(extr, getString(R.string.free_tiket)+".jpg");
    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) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    v iz root layout...just to point ;)))

    ReplyDelete
  2. Never got the answer I was looking for the code provided did work for screenshoting my own app however what i wanted was to do it with root outside my app. I then realized cm natively had screenshoting so i took a look at the rom and realized there was a binary they just ran in a shell and it captured the screen so i just compiled it myself and ended up using that

    Thank you everyone for the help

    ReplyDelete
  3. I think you have to wait until the layout is drawn completely..Use ViewTreeObserver to get a call back when layout is drawn completely..

    On your onCreate add this code..Only call getScreen from inside onGlobalLayout()..

    ViewTreeObserver vto = content.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
    content.getViewTreeObserver().removeGlobalOnLayoutListener(this);
    getScreen();
    }
    });


    I asked a somewhat similiar question once..Please see my question which explains the way to take screenshot in android..Hope this helps

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex