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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.