android - How to enlarge a picture in Gridview when it is clicked using a new activity? -
so have activity inflates gridview, , want clickable, , upon click, opens new activity, shows whole image, along texts i've written in activity. note confirmed txt file written written successfully, , in sd card.
here code gridview activity
public class gridview extends appcompatactivity { android.widget.gridview gv; arraylist<file> list,list2; @override protected void oncreate(@nullable bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.gallery); file dir = new file(environment.getexternalstoragedirectory().getabsolutepath() + "/mycameraapp"); file dir2 = new file(environment.getexternalstoragedirectory().getabsolutepath() + "/mycameraapp"); list = imagereader(dir); list2 = locationreader(dir2); gv = (android.widget.gridview) findviewbyid(r.id.gridview); gv.setadapter(new gridadapter()); gv.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { intent intent = new intent(gridview.this, viewimage.class); intent.putextra("myimage", list.get(position)); intent.putextra("mylocation", list2.get(position)); startactivity(intent); //startactivity(new intent(getapplicationcontext(), viewimage.class).putextra("img",list.get(position))); } }); } public void backtomain(view view) { intent backtomain = new intent(this, mainactivity.class); startactivity(backtomain); } class gridadapter extends baseadapter{ @override public int getcount() { return list.size(); } @override public object getitem(int position) { return list.get(position); } @override public long getitemid(int position) { return 0; } @override public view getview(int position, view convertview, viewgroup parent) { convertview = getlayoutinflater().inflate(r.layout.simple_grid, parent, false); imageview iv = (imageview) convertview.findviewbyid(r.id.imageview); iv.setimageuri(uri.parse(getitem(position).tostring())); return convertview; } } arraylist<file> imagereader(file root){ arraylist<file> = new arraylist<>(); file[] files = root.listfiles(); for(int = 0; < files.length; i++){ if(files[i].isdirectory()){ } else { if(files[i].getname().endswith(".jpg")){ a.add(files[i]); } } } return a; } arraylist<file> locationreader(file root){ arraylist<file> = new arraylist<>(); file[] files = root.listfiles(); for(int = 0; < files.length; i++){ if(files[i].isdirectory()){ } else { if(files[i].getname().endswith(".txt")){ a.add(files[i]); } } } return a; } }
here activity that's called gridview, supposedly show image plus open file i've written earlier, , show content (a string).
public class viewimage extends appcompatactivity { imageview imageview; textview textview; @override protected void oncreate(@nullable bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.view_image); // int image = getintent().getextras().getint("myimage", r.drawable.common_google_signin_btn_text_light); int image = getintent().getintextra("myimage", r.drawable.cast_ic_notification_0); imageview imageview = (imageview) findviewbyid(r.id.imageview2); imageview.setimageresource(image); intent calledactivity = getintent(); string pos = calledactivity.getextras().getstring("mylocation"); textview = (textview) findviewbyid(r.id.text_location); textview.settext(pos); } public void backtocamera(view view) { intent intent = new intent(viewimage.this, mainactivity.class); startactivity(intent); } }
right now, see r.drawable.cast_ic_notification_0 being picture, opposed gridview's image. also, textview empty, used debugger , showed
string pos = calledactivity.getextras().getstring("mylocation");
pos null. please help!
list.get(position)
file , you're trying read getextras().getint()
i think should pass file path string:
intent.putextra("myimage", list.get(position).getabsolutepath());
and receive it:
file imgfile = new file(getintent().getstringextra("myimage")); if(imgfile.exists()){ bitmap mybitmap = bitmapfactory.decodefile(imgfile.getabsolutepath()); imageview imageview = (imageview) findviewbyid(r.id.imageview2); imageview.setimagebitmap(mybitmap); }
Comments
Post a Comment