android - How to save the current image from viewpager? -
i have images in viewpager
, want user can save current image click on button save image sd card in phone
this demoactivity.java
public class demoactivity extends activity { button play; mediaplayer mp; public int playstop = 0; private button extisaw; public void oncreate(bundle ww) { super.oncreate(ww); setcontentview(r.layout.activity_main); play = (button) findviewbyid(r.id.saw); mp = mediaplayer.create(this, r.raw.raj); show1(); show2(); play.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (playstop == 0) { mediahandler(mp, 1); mp.start(); playstop = 1; } else { mediahandler(mp, 2); mp.pause(); playstop = 0; } } }); } private void show2() { // todo: implement method this.extisaw = (button)this.findviewbyid (r.id.about); this.extisaw.setonclicklistener (new onclicklistener() { @override public void onclick(view v) { finish(); } });} public void mediahandler (mediaplayer mp, int e) { if (e == 1) { mp.start(); } else { mp.pause(); } } public void show1() { viewpager viewpager = (viewpager) findviewbyid(r.id.view_pager); imagepageradapter adapter = new imagepageradapter(); viewpager.setadapter(adapter); } private class imagepageradapter extends pageradapter { private int[] mimages = new int[]{ r.drawable.a1, r.drawable.a2, r.drawable.a3, r.drawable.a4, r.drawable.a5, r.drawable.a6, r.drawable.a7, r.drawable.a8 , r.drawable.a9, r.drawable.a10, r.drawable.a11, r.drawable.a12 , r.drawable.a13, r.drawable.a14, r.drawable.a15 , r.drawable.a16, r.drawable.a17, r.drawable.a18, r.drawable.a19 , r.drawable.a20, r.drawable.a21, r.drawable.a22, r.drawable.a23 , r.drawable.a24, r.drawable.a25, r.drawable.a26, r.drawable.a27 , r.drawable.a29, r.drawable.a30, r.drawable.a31 , r.drawable.a32, r.drawable.a33, r.drawable.a34, r.drawable.a35 , r.drawable.a36, r.drawable.a37, r.drawable.a38 , r.drawable.a39, r.drawable.a40, r.drawable.a42 , r.drawable.a43, r.drawable.a44, r.drawable.a45, r.drawable.a46, r.drawable.a47, r.drawable.a48, r.drawable.a49 , r.drawable.a51, r.drawable.a52, r.drawable.a53 , r.drawable.a54, r.drawable.a55, r.drawable.a56 , r.drawable.a58, r.drawable.a59, r.drawable.a60 , r.drawable.a61, r.drawable.a62, r.drawable.a63 , r.drawable.a64, r.drawable.a65, r.drawable.a66, r.drawable.a67, r.drawable.a68, r.drawable.a69 , r.drawable.a70, r.drawable.a71, r.drawable.a72 , r.drawable.a73, r.drawable.a74, r.drawable.a75 , r.drawable.a76, r.drawable.a77, r.drawable.a78 , r.drawable.a79, r.drawable.a80, r.drawable.a81 , r.drawable.a82, r.drawable.a83, r.drawable.a84, r.drawable.a85, r.drawable.a86, r.drawable.a87 , r.drawable.a88, r.drawable.a89, r.drawable.a90 , r.drawable.a91, r.drawable.a93 , r.drawable.a94, r.drawable.a95, r.drawable.a96 , r.drawable.a97, r.drawable.a98, r.drawable.a99 }; @override public int getcount() { return mimages.length; } @override public boolean isviewfromobject(view view, object object) { return view == ((imageview) object); } @override public object instantiateitem(viewgroup container, int position) { context context = demoactivity.this; imageview imageview = new imageview(context); int padding = context.getresources().getdimensionpixelsize( r.dimen.padding_medium); imageview.setpadding(padding, padding, padding, padding); imageview.setscaletype(imageview.scaletype.center_inside); imageview.setimageresource(mimages[position]); ((viewpager) container).addview(imageview, 0); return imageview; } @override public void destroyitem(viewgroup container, int position, object object) { ((viewpager) container).removeview((imageview) object); } }
}
i add code activity class try this..
public class demoactivity extends activity { button play; mediaplayer mp; private int[] mimages = new int[]{ r.drawable.a1, r.drawable.a2, r.drawable.a3, r.drawable.a4, r.drawable.a5, r.drawable.a6, r.drawable.a7, r.drawable.a8 , r.drawable.a9, r.drawable.a10, r.drawable.a11, r.drawable.a12 , r.drawable.a13, r.drawable.a14, r.drawable.a15 , r.drawable.a16, r.drawable.a17, r.drawable.a18, r.drawable.a19 , r.drawable.a20, r.drawable.a21, r.drawable.a22, r.drawable.a23 , r.drawable.a24, r.drawable.a25, r.drawable.a26, r.drawable.a27 , r.drawable.a29, r.drawable.a30, r.drawable.a31 , r.drawable.a32, r.drawable.a33, r.drawable.a34, r.drawable.a35 , r.drawable.a36, r.drawable.a37, r.drawable.a38 , r.drawable.a39, r.drawable.a40, r.drawable.a42 , r.drawable.a43, r.drawable.a44, r.drawable.a45, r.drawable.a46, r.drawable.a47, r.drawable.a48, r.drawable.a49 , r.drawable.a51, r.drawable.a52, r.drawable.a53 , r.drawable.a54, r.drawable.a55, r.drawable.a56 , r.drawable.a58, r.drawable.a59, r.drawable.a60 , r.drawable.a61, r.drawable.a62, r.drawable.a63 , r.drawable.a64, r.drawable.a65, r.drawable.a66, r.drawable.a67, r.drawable.a68, r.drawable.a69 , r.drawable.a70, r.drawable.a71, r.drawable.a72 , r.drawable.a73, r.drawable.a74, r.drawable.a75 , r.drawable.a76, r.drawable.a77, r.drawable.a78 , r.drawable.a79, r.drawable.a80, r.drawable.a81 , r.drawable.a82, r.drawable.a83, r.drawable.a84, r.drawable.a85, r.drawable.a86, r.drawable.a87 , r.drawable.a88, r.drawable.a89, r.drawable.a90 , r.drawable.a91, r.drawable.a93 , r.drawable.a94, r.drawable.a95, r.drawable.a96 , r.drawable.a97, r.drawable.a98, r.drawable.a99 }; public int playstop = 0; private button extisaw; private imagepageradapter adapter; private viewpager viewpager; public void oncreate(bundle ww) { super.oncreate(ww); setcontentview(r.layout.activity_main); play = (button) findviewbyid(r.id.saw); mp = mediaplayer.create(this, r.raw.raj); show1(); show2(); play.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (playstop == 0) { mediahandler(mp, 1); mp.start(); playstop = 1; } else { mediahandler(mp, 2); mp.pause(); playstop = 0; } } }); } private void show2() { // todo: implement method this.extisaw = (button)this.findviewbyid (r.id.about); this.extisaw.setonclicklistener (new onclicklistener() { @override public void onclick(view v) { finish(); } }); } public void mediahandler (mediaplayer mp, int e) { if (e == 1) { mp.start(); } else { mp.pause(); } } private void saveimageintosdcard(bitmap bitmap) { file filename = null; fileoutputstream out = null; string destination; try { string path = environment.getexternalstoragedirectory().tostring(); destination ="/destinationfolder name/"; file sdcarddirectory = new file(environment.getexternalstoragedirectory(),destination); filename = new file(sdcarddirectory, system.currenttimemillis()+"image.jpg"); out = new fileoutputstream(filename); bitmap.compress(bitmap.compressformat.jpeg, 100, out); mediastore.images.media.insertimage(getcontentresolver(), filename.getabsolutepath(), filename.getname(), filename.getname()); } catch (exception e) { e.printstacktrace(); } { if (out != null) { try { out.flush(); out.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } } } public void show1() { viewpager = (viewpager) findviewbyid(r.id.view_pager); adapter = new imagepageradapter(); viewpager.setadapter(adapter); button b1 = (button) findviewbyid(r.id.save); b1.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { int position=viewpager.getcurrentitem(); int selectedimage=mimages[position]; bitmap largeicon = bitmapfactory.decoderesource(getresources(),selectedimage); saveimageintosdcard(largeicon); } }); } private class imagepageradapter extends pageradapter { @override public int getcount() { return mimages.length; } @override public boolean isviewfromobject(view view, object object) { return view == ((imageview) object); } @override public object instantiateitem(viewgroup container, int position) { context context = demoactivity.this; imageview imageview = new imageview(context); int padding = context.getresources().getdimensionpixelsize( r.dimen.padding_medium); imageview.setpadding(padding, padding, padding, padding); imageview.setscaletype(imageview.scaletype.center_inside); imageview.setimageresource(mimages[position]); ((viewpager) container).addview(imageview, 0); return imageview; } @override public void destroyitem(viewgroup container, int position, object object) { ((viewpager) container).removeview((imageview) object); } } }
Comments
Post a Comment