How to capture screen with coding

Put your problem here if it does not fit any of the other categories.

How to capture screen with coding

Postby zhufan » Wed Nov 26, 2008 3:40 am

Hello!!everyone,
is there anyway to capture screen?, I find some fuctions :
View v=tv.getRootView();
v.setDrawingCacheEnabled(true);
Bitmap bm= v.getDrawingCache();
why bm==null? help!help!
zhufan
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed Nov 26, 2008 3:18 am

Top

Postby MrSnowflake » Wed Nov 26, 2008 11:44 am

V should be in the ContentView of the activity, otherwise you won't be able to capture the view. Maybe you could try to manually draw the view.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby zhufan » Wed Nov 26, 2008 1:19 pm

MrSnowflake wrote:V should be in the ContentView of the activity, otherwise you won't be able to capture the view. Maybe you could try to manually draw the view.



thanks,MrSnowflake,I can get the screen bitmap and draw it in imageview
zhufan
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed Nov 26, 2008 3:18 am

Postby beaster » Thu Nov 27, 2008 12:21 am

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. setContentView(R.layout.main);
  3.  
  4. TextView p=findViewById(r.id.text);
  5.  
  6. p.setDrawingCacheEnabled(true);
  7.  
  8. Bitmap bm= p.getDrawingCache();
  9.  
  10.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

bm-null!!!!!
Help!!!!!!!!
beaster
Junior Developer
Junior Developer
 
Posts: 16
Joined: Tue Oct 07, 2008 10:43 am

Postby zhufan » Thu Nov 27, 2008 4:11 pm

beaster wrote:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. setContentView(R.layout.main);
  2. TextView p=findViewById(r.id.text);
  3. p.setDrawingCacheEnabled(true);
  4. Bitmap bm= p.getDrawingCache();
  5.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

bm-null!!!!!
Help!!!!!!!!



p should be in the ContentView of the activity, otherwise you won't be able to get bitmap of the view
zhufan
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed Nov 26, 2008 3:18 am

How to get the ContentView of the activity

Postby survivedev » Mon Dec 01, 2008 3:16 pm

the problem is how to get the ContentView of the activity?
it seems I can't get it by GetCurrentFocus() or GetWindow.GetCurrentFocus().
Thanks.

zhufan wrote:
beaster wrote:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. setContentView(R.layout.main);
  2. TextView p=findViewById(r.id.text);
  3. p.setDrawingCacheEnabled(true);
  4. Bitmap bm= p.getDrawingCache();
  5.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

bm-null!!!!!
Help!!!!!!!!



p should be in the ContentView of the activity, otherwise you won't be able to get bitmap of the view
survivedev
Freshman
Freshman
 
Posts: 3
Joined: Mon Dec 01, 2008 2:59 pm

Top

Postby zhufan » Tue Dec 02, 2008 4:46 am

Hi ,survivedev
why do you get the content view? if you want to get some view of activity,you can use function "findViewById".
you can use Android' Development Tools:Hierarchy Viewer in tools folder,and then you can see the layout's hierarchy of Views in your activity
zhufan
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed Nov 26, 2008 3:18 am

Postby survivedev » Tue Dec 02, 2008 4:57 am

hi, zhufan

Thanks a lot for the reply.
it seems I make the problem more complex.
let's see the code:

// from the Hello Activity of the google's example. I just want to capture current screen.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Set the layout for this activity. You can find it
// in res/layout/hello_activity.xml
setContentView(R.layout.hello_activity); // hello_activity have been defined in the layout xml.
View v = this.findViewById(R.layout.hello_activity); // get current contentview
if( v != null)
{
v.setDrawingCacheEnabled(true);
v.buildDrawingCache();
Bitmap bm = v.getDrawingCache(); // in here the bm is a null value.
try
{
if ( bm != null )
{
/// save to a file
}
}
}

}

however, the bm I get is still null.
so, I'm not quite understand "the contentview of the activity" in your post.
would u like to give some more hints?
thanks a lot!

zhufan wrote:Hi ,survivedev
why do you get the content view? if you want to get some view of activity,you can use function "findViewById".
you can use Android' Development Tools:Hierarchy Viewer in tools folder,and then you can see the layout's hierarchy of Views in your activity
survivedev
Freshman
Freshman
 
Posts: 3
Joined: Mon Dec 01, 2008 2:59 pm

Postby zhufan » Tue Dec 02, 2008 6:19 am

View v = this.findViewById(R.layout.hello_activity);
sure v!=null ? maybe u can use getrootview to get the top view

setContentView(R.layout.main);
bn=(Button)findViewById(R.id.bn);
iv=(ImageView)findViewById(R.id.iv);
bn.setOnClickListener(new OnClickListener(){

public void onClick(View v) {
// TODO Auto-generated method stub
View v1=bn.getRootView();
v1.setDrawingCacheEnabled(true);
bm=v1.getDrawingCache();
if(bm!=null){
iv.setImageBitmap(bm);
iv.invalidate();
outputpicture();
}
}

});
zhufan
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed Nov 26, 2008 3:18 am

Postby survivedev » Tue Dec 02, 2008 1:07 pm

many thanks.
it works. :)

zhufan wrote:View v = this.findViewById(R.layout.hello_activity);
sure v!=null ? maybe u can use getrootview to get the top view

setContentView(R.layout.main);
bn=(Button)findViewById(R.id.bn);
iv=(ImageView)findViewById(R.id.iv);
bn.setOnClickListener(new OnClickListener(){

public void onClick(View v) {
// TODO Auto-generated method stub
View v1=bn.getRootView();
v1.setDrawingCacheEnabled(true);
bm=v1.getDrawingCache();
if(bm!=null){
iv.setImageBitmap(bm);
iv.invalidate();
outputpicture();
}
}

});
survivedev
Freshman
Freshman
 
Posts: 3
Joined: Mon Dec 01, 2008 2:59 pm

Postby Danuubz » Mon Jan 12, 2009 5:21 pm

It simply works with:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Bitmap screenshot;
  3.  
  4.  
  5.  
  6. main.setDrawingCacheEnabled(true);
  7.  
  8. screenshot = Bitmap.createBitmap(main.getDrawingCache());
  9.  
  10. main.setDrawingCacheEnabled(false);
  11.  
  12.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


main is the content view in this case. You need Bitmap.createBitmap(main.getDrawingCache()); as the Bitmap from which the reference is received by getDrawingCache() gets recycled.
Sticky Note Cork&Orc [Android Market] : http://www.youtube.com/watch?v=ewWaquAwZQY
User avatar
Danuubz
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Wed Dec 19, 2007 10:44 pm
Location: Germany

Postby JoyLakh » Mon Nov 23, 2009 2:08 pm

Thanks Danuubz, u saved my time.
Celebrate Life.
JoyLakh
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue Dec 16, 2008 5:42 am
Location: Bangalore, India

Postby uzair77 » Tue Apr 20, 2010 2:20 pm

the capture image save in gallery ?
uzair77
Junior Developer
Junior Developer
 
Posts: 24
Joined: Wed Mar 24, 2010 10:57 am

Postby uzair77 » Thu Apr 22, 2010 8:33 am

Thanks for this
can u tell me what is this
iv=(ImageView)findViewById(R.id.iv);



Danuubz wrote:It simply works with:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Bitmap screenshot;
  2.  
  3. main.setDrawingCacheEnabled(true);
  4. screenshot = Bitmap.createBitmap(main.getDrawingCache());
  5. main.setDrawingCacheEnabled(false);
  6.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


main is the content view in this case. You need Bitmap.createBitmap(main.getDrawingCache()); as the Bitmap from which the reference is received by getDrawingCache() gets recycled.
uzair77
Junior Developer
Junior Developer
 
Posts: 24
Joined: Wed Mar 24, 2010 10:57 am

Screen capture

Postby kapilbhawsar » Tue May 04, 2010 7:33 am

Hi,

Here is complete code :


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class ScreenCapture extends Activity{
  2.  
  3.         LinearLayout view;
  4.  
  5.         @Override
  6.  
  7.         protected void onCreate(Bundle savedInstanceState) {
  8.  
  9.                 // TODO Auto-generated method stub
  10.  
  11.                 super.onCreate(savedInstanceState);
  12.  
  13.                 setContentView(R.layout.main);
  14.  
  15.                
  16.  
  17.                 view = (LinearLayout)findViewById(R.id.screen);
  18.  
  19.                 Button myBtn = (Button)findViewById(R.id.myBtn);
  20.  
  21.                
  22.  
  23.                 myBtn.setOnClickListener(new View.OnClickListener(){
  24.  
  25.                         @Override
  26.  
  27.                         public void onClick(View v) {
  28.  
  29.                                 // TODO Auto-generated method stub
  30.  
  31.                                 View v1 = view.getRootView();
  32.  
  33.                                 System.out.println("Root View : "+v1);
  34.  
  35.                                 v1.setDrawingCacheEnabled(true);
  36.  
  37.                                 Bitmap bm = v1.getDrawingCache();
  38.  
  39.                                
  40.  
  41.                                 System.out.println("Bitmap : "+bm);
  42.  
  43.                                 showScreen(bm);
  44.  
  45.                         }
  46.  
  47.                 });
  48.  
  49.                
  50.  
  51.         }
  52.  
  53. }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
kapilbhawsar
Once Poster
Once Poster
 
Posts: 1
Joined: Tue May 04, 2010 7:29 am
Location: IN

Top
Next

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 28 guests