Friday 15 July 2011

screenshots in android programmatically -



screenshots in android programmatically -

i want take screenshot in android activity through programme when user click on button take screenshot, screenshot shoudl saved in sd card. adding next code , facing errors.in eclipse bundle name in code underlined reddish line tostring(com.meri.meri_application).

public class partysalesandrecovery extends activity implements onclicklistener{ userfunctions userfunction; databasehandler db; string str; string moneystring2,moneystring3,moneystring4,moneystring5,moneystring6,moneystring7; string month,loginname,week; imageview imageview; bitmap bmp; int orientation; int total_sale_trg = 0; int total_sale_act = 0; int total_rec_trg = 0; int total_rec_act = 0; int total_per_sale = 0; int total_per_rec = 0; private static string key_success = "success"; tablelayout tl; layoutparams lp; spinner quesweek,quesmonth; button btnshow,btntakescreen; jsonobject json,json_user; private view view; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.partysalesandrecovery); view= findviewbyid(r.id.screenroot); orientation = this.getresources().getconfiguration().orientation; calendar cal=calendar.getinstance(); simpledateformat month_date = new simpledateformat("mmmmmmmmm"); string month_name = month_date.format(cal.gettime()); quesmonth = (spinner)findviewbyid(r.id.combomonth); quesweek = (spinner)findviewbyid(r.id.comboweek); arrayadapter myadap = (arrayadapter) quesmonth.getadapter(); int spinnerposition = myadap.getposition(month_name); quesmonth.setselection(spinnerposition); tl = (tablelayout)findviewbyid(r.id.tablelayoutlocationsaleandrecovery); btnshow = (button)findviewbyid(r.id.btnshowreports); btnshow.setonclicklistener(this); btntakescreen = (button)findviewbyid(r.id.btntakescreenshot); btntakescreen.setonclicklistener(this); } /////////////////////////////// /////////////////////////////////////// public void savebitmap(bitmap bitmap) { seek { bytearrayoutputstream bytes = new bytearrayoutputstream(); bitmap.compress(bitmap.compressformat.jpeg, 40, bytes); file f = new file(environment.getexternalstoragedirectory() + file.separator + "test.jpg"); f.createnewfile(); fileoutputstream fo = new fileoutputstream(f); fo.write(bytes.tobytearray()); fo.close(); } grab (filenotfoundexception e) { log.e("grec", e.getmessage(), e); }catch (ioexception e) { log.e("grec", e.getmessage(), e); } } //////////////////////////////// @override public void onclick(view v) { // todo auto-generated method stub if(v.getid()==r.id.btntakescreenshot) { bitmap bitmap = bitmap.createbitmap(500, 500, bitmap.config.alpha_8); view = (linearlayout)findviewbyid(r.id.viewroot); bitmap result = drawviewtobitmap(from, bitmap); imageview = (imageview)findviewbyid(r.id.imgviewpic); imageview.setimagebitmap(result); savebitmap(bitmap); seek { imageview.setdrawingcacheenabled(true); bmp = bitmap.createbitmap(imageview.getdrawingcache()); imageview.setdrawingcacheenabled(false); bytearrayoutputstream bytes = new bytearrayoutputstream(); bmp.compress(bitmap.compressformat.jpeg, 40, bytes); file f = new file(environment.getexternalstoragedirectory() + file.separator + "test.jpg"); f.createnewfile(); fileoutputstream fo = new fileoutputstream(f); fo.write(bytes.tobytearray()); fo.close(); } grab (exception e) { // todo auto-generated grab block e.printstacktrace(); toast.maketext(this, e.getmessage(), 7000).show(); } } code of xml <?xml version="1.0" encoding="utf-8"?> <scrollview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/viewroot" > <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/combomonth" android:prompt="@string/month_prompt" android:entries="@array/question_months" android:layout_weight="0.5" /> <spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/comboweek" android:entries="@array/question_weeks" android:prompt="@string/week_prompt" android:layout_weight="0.5" /> </linearlayout> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="show reports" android:id="@+id/btnshowreports" /> <view android:layout_width="fill_parent" android:layout_height="1dp" android:background="#000000" /> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:text="projects sales , recovery status" android:textsize="16sp" android:textstyle="bold" android:gravity="center" /> <tablelayout android:id="@+id/tablelayoutlocationsaleandrecovery" android:layout_width="match_parent" android:layout_height="match_parent" android:shrinkcolumns="*" android:stretchcolumns="*" > </tablelayout> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="capture" android:layout_gravity="right" android:id="@+id/btntakescreenshot" /> <imageview android:id="@+id/imgviewpic" android:layout_width="250dp" android:layout_height="250dp" android:src="@drawable/ic_launcher" android:layout_gravity="center" android:visibility="invisible" /> </linearlayout> </scrollview>

just replace save bitmap method this

public void savebitmap(bitmap bitmap) { seek { bytearrayoutputstream bytes = new bytearrayoutputstream(); bitmap.compress(bitmap.compressformat.jpeg, 40, bytes); file f = new file(environment.getexternalstoragedirectory() + file.separator + "test.jpg"); f.createnewfile(); fileoutputstream fo = new fileoutputstream(f); fo.write(bytes.tobytearray()); fo.close(); } grab (filenotfoundexception e) { log.e("grec", e.getmessage(), e); } grab (ioexception e) { log.e("grec", e.getmessage(), e); } }

edit 1:

just replcae code code

@override public void onclick(view v) { // todo auto-generated method stub if (v.getid() == r.id.btntakescreenshot) { bitmap bitmap = takescreenshot() imageview.setimagebitmap(bitmap); savebitmap(bitmap); } } public bitmap takescreenshot() { view rootview = findviewbyid(android.r.id.content).getrootview(); rootview.setdrawingcacheenabled(true); homecoming rootview.getdrawingcache(); } public void savebitmap(bitmap bitmap) { seek { bytearrayoutputstream bytes = new bytearrayoutputstream(); bitmap.compress(bitmap.compressformat.jpeg, 40, bytes); file f = new file(environment.getexternalstoragedirectory() + file.separator + "test.jpg"); f.createnewfile(); fileoutputstream fo = new fileoutputstream(f); fo.write(bytes.tobytearray()); fo.close(); } grab (filenotfoundexception e) { log.e("grec", e.getmessage(), e); } grab (ioexception e) { log.e("grec", e.getmessage(), e); } }

android screenshot

No comments:

Post a Comment