i want make viewpager , when user click on photo album , able scroll between images viewpager . need . answer apreciate, thanks. code:
viewimage class photo showed in full-screen :
public class viewimage extends activity { // declare variable textview text; imageview imageview; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // view view_image.xml setcontentview(r.layout.view_image); // retrieve data mainactivity on gridview item click intent = getintent(); // position int position = i.getextras().getint("position"); // string arrays filepathstrings string[] filepath = i.getstringarrayextra("filepath"); // string arrays filenamestrings string[] filename = i.getstringarrayextra("filename"); // locate textview in view_image.xml text = (textview) findviewbyid(r.id.imagetext); // load text textview followed position text.settext(filename[position]); // locate imageview in view_image.xml imageview = (imageview) findviewbyid(r.id.full_image_view); // decode filepath bitmapfactory followed position bitmap bmp = bitmapfactory.decodefile(filepath[position]); // set decoded bitmap imageview imageview.setimagebitmap(bmp); } }
albumactivity class , created new album:
public class albumactivity extends activity implements appcompatcallback { private appcompatdelegate delegate1; private final int request_code_camera_image = 1000; private final int request_code_external_image = 2000; string namealbum; // declare variables private string[] filepathstrings; private string[] filenamestrings; private file[] listfile; gridview grid; gridviewadapter adapter; file file; boolean deleted; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); delegate1 = appcompatdelegate.create(this, this); //call oncreate() of appcompatdelegate delegate1.oncreate(savedinstancestate); //use delegate inflate layout delegate1.setcontentview(r.layout.album_activity); toolbar toolbar = (toolbar) findviewbyid(r.id.mytoolbarr); delegate1.setsupportactionbar(toolbar); delegate1.settitle("your pictures"); button btnchoosepicture = (button) findviewbyid(r.id.addimage); intent intent = getintent(); namealbum = intent.getstringextra("namealbum"); // check sd card if (!environment.getexternalstoragestate().equals( environment.media_mounted)) { toast.maketext(this, "error! no sdcard found!", toast.length_long) .show(); } else { // locate image folder in sd card file = new file(environment.getexternalstoragedirectory() + file.separator + namealbum); if (file.isdirectory()) { listfile = file.listfiles(); // create string array filepathstrings filepathstrings = new string[listfile.length]; // create string array filenamestrings filenamestrings = new string[listfile.length]; (int = 0; < listfile.length; i++) { // path of image file filepathstrings[i] = listfile[i].getabsolutepath(); // name image file filenamestrings[i] = listfile[i].getname(); } } // locate gridview in gridview_main.xml grid = (gridview) findviewbyid(r.id.gridview); // pass string arrays lazyadapter class adapter = new gridviewadapter(this, filepathstrings, filenamestrings); // set lazyadapter gridview grid.setadapter(adapter); // capture gridview item click grid.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { intent = new intent(albumactivity.this, viewimage.class); // pass string arrays filepathstrings i.putextra("filepath", filepathstrings); // pass string arrays filenamestrings i.putextra("filename", filenamestrings); // pass click position i.putextra("position", position); startactivity(i); } }); grid.setonitemlongclicklistener(new adapterview.onitemlongclicklistener() { @override public boolean onitemlongclick(adapterview<?> parent, final view view, final int position, final long id) { alertdialog.builder builder = new alertdialog.builder(albumactivity.this); builder.setcancelable(true); builder.setmessage("are sure want delete picture ?"); builder.setneutralbutton("cancel", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); builder.setpositivebutton("delete", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { file filepath = environment.getexternalstoragedirectory(); file dir5 = new file(filepath.getabsolutepath() + namealbum+filenamestrings[position]); file file3 = new file(string.valueof(dir5)); deleted = file3.delete(); adapter.notifydatasetchanged(); finish(); startactivity(getintent()); dialog.dismiss(); } }); builder.settitle("delete picture"); alertdialog dialog = builder.create(); dialog.show(); return true; } }); } //select picture external storage btnchoosepicture.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // choose picture gallery intent photopickerintent = new intent( intent.action_pick); file picturedirectory = environment.getexternalstoragepublicdirectory(environment.directory_pictures); string picturedirectorypath = picturedirectory.getpath(); uri data = uri.parse(picturedirectorypath); photopickerintent.setdataandtype(data, "image/*"); startactivityforresult(photopickerintent, request_code_external_image); } }); } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_setting) { return true; }else if(id==android.r.id.home){ finish(); } return super.onoptionsitemselected(item); } public void onactivityresult(int requestcode, int resultcode, intent data) { // image external storage if(resultcode == result_ok){ if (requestcode == request_code_external_image) { uri uri = data.getdata(); inputstream inputstream; try { inputstream = getcontentresolver().openinputstream(uri); bitmap image = bitmapfactory.decodestream(inputstream); arraylist<bitmap> bitmaparray = new arraylist<bitmap>(); bitmaparray.add(image); outputstream output; // find sd card path file filepath = environment.getexternalstoragedirectory(); int count2 =0; string paranteze = "("+count2+")"; int count1 = new file("/mnt/sdcard" + namealbum).listfiles().length; count1 = count1 + 1; string nameimage = "wallpaper_" +count1 +".jpg"; for(int j =0;j<listfile.length;j++){ filenamestrings[j] = listfile[j].getname(); if(filenamestrings[j].equals(nameimage)){ nameimage="wallpaper_" +count1+paranteze +".jpg"; count2++; } } count2++; file dir = new file(filepath.getabsolutepath() + namealbum); // retrieve image res folder // create name saved image file = new file(dir, nameimage); try { inputstream = getcontentresolver().openinputstream(uri); bitmap bitmap = bitmapfactory.decodestream(inputstream); output = new fileoutputstream(file); // compress png format image 0% - 100% bitmap.compress(bitmap.compressformat.jpeg, 100, output); output.flush(); output.close(); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } catch (filenotfoundexception e) { e.printstacktrace(); } } if (!environment.getexternalstoragestate().equals( environment.media_mounted)) { toast.maketext(this, "error! no sdcard found!", toast.length_long) .show(); } else { // locate image folder in sd card file = new file(environment.getexternalstoragedirectory() + file.separator + namealbum); if (file.isdirectory()) { listfile = file.listfiles(); // create string array filepathstrings filepathstrings = new string[listfile.length]; // create string array filenamestrings filenamestrings = new string[listfile.length]; (int = 0; < listfile.length; i++) { // path of image file filepathstrings[i] = listfile[i].getabsolutepath(); // name image file filenamestrings[i] = listfile[i].getname(); // locate gridview in gridview_main.xml grid = (gridview) findviewbyid(r.id.gridview); // pass string arrays lazyadapter class adapter = new gridviewadapter(this, filepathstrings, filenamestrings); // set lazyadapter gridview grid.setadapter(adapter); adapter.notifydatasetchanged(); } } } } } @override public void onsupportactionmodestarted(actionmode mode) { } @override public void onsupportactionmodefinished(actionmode mode) { } @nullable @override public actionmode onwindowstartingsupportactionmode(actionmode.callback callback) { return null; } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_album_activity, menu); return true; } @override public boolean onmenuitemselected(int featureid, menuitem item) { switch (item.getitemid()){ case r.id.action_add: // choose picture gallery intent photopickerintent = new intent( intent.action_pick); file picturedirectory = environment.getexternalstoragepublicdirectory(environment.directory_pictures); string picturedirectorypath = picturedirectory.getpath(); uri data = uri.parse(picturedirectorypath); photopickerintent.setdataandtype(data, "image/*"); startactivityforresult(photopickerintent, request_code_external_image); break; case r.id.action_delete: break; } return super.onmenuitemselected(featureid, item); } }
and of course gridviewadapter class
public class gridviewadapter extends baseadapter { imageview image; // declare variables private activity activity; private string[] filepath; private string[] filename; private static layoutinflater inflater = null; public gridviewadapter(activity a, string[] fpath, string[] fname) { activity = a; filepath = fpath; filename = fname; inflater = (layoutinflater) activity .getsystemservice(context.layout_inflater_service); } public int getcount() { return filepath.length; } public object getitem(int position) { return position; } public long getitemid(int position) { return position; } public view getview(int position, view convertview, viewgroup parent) { view vi = convertview; if (convertview == null) vi = inflater.inflate(r.layout.gridview_item, null); // locate textview in gridview_item.xml textview text = (textview) vi.findviewbyid(r.id.text); // locate imageview in gridview_item.xml image = (imageview) vi.findviewbyid(r.id.grid_image); // set file name textview followed position file file = new file(filepath[position]); picasso.with(activity).load(file).placeholder(r.drawable.rtrt).fit().centercrop().into(image); // decode filepath bitmapfactory followed position // set decoded bitmap imageview // image.setimagebitmap(bmp); return vi; }
}
Comments
Post a Comment