Display image in imageview in android -
i've set onclicklistener
on imageview layout , trying display image phone. everywhere looked process , similar. should work image not displaying in imageview. maybe i'm not handling in right way happening in fragment. in fragment code displaying image. need suggestion overcome.
public class userprofile extends fragment { private imageview profileimg; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.fragment_user_profile, container, false); profileimg = (imageview) v.findviewbyid(r.id.imageview_profile); profileimg.setonclicklistener(chngimghandler); return v; } onclicklistener chngimghandler = new onclicklistener() { @override public void onclick(view v) { intent intent = new intent(); intent.settype("image/*"); intent.setaction(intent.action_get_content); startactivityforresult(intent.createchooser(intent, "complete action using"), 1); } }; public void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == 1 && resultcode == getactivity().result_ok) { // bitmap photo = (bitmap) data.getdata().getpath(); uri selectedimageuri = data.getdata(); imagepath = getpath(selectedimageuri); bitmap bitmap = bitmapfactory.decodefile(imagepath); profileimg.setimagebitmap(bitmap); } } public string getpath(uri uri) { string[] projection = { mediastore.images.media.data }; @suppresswarnings("deprecation") cursor cursor = getactivity().managedquery(uri, projection, null, null, null); int column_index = cursor.getcolumnindexorthrow(mediastore.images.media.data); cursor.movetofirst(); return cursor.getstring(column_index); }
layout of user profile
<linearlayout android:layout_width="match_parent" android:layout_height="200dp" android:background="@color/profilepicbg" android:orientation="vertical" > <imageview android:id="@+id/imageview_profile" android:layout_width="120dp" android:layout_height="100dp" android:layout_gravity="center" android:layout_margintop="25dp" android:src="@drawable/ic_launcher" /> <button android:id="@+id/btnchangepicture" android:layout_width="100dp" android:layout_height="40dp" android:layout_gravity="center" android:layout_margintop="15dp" android:text="you?" android:textcolor="#fff" android:textsize="12sp" /> </linearlayout>
is inner class? because don't see declaration profileimg
anywhere. try adding:
private imageview profileimg;
..on top of class. or, if isn't case try adding:
intent.putextra("return-data", true);
..to onclick
method declaration.
Comments
Post a Comment