I have one fragment and one activity.. In my layout i have one image view and one button. I wrote some coding for pick image from galley but i cant place that image on imageView.. I dont Konw how to do. Please any one fix this issue.
My code here:
ProfileFragment.java
public class ProfileFragment extends Fragment {
private ImageView imageView;
private static final int SELECT_PHOTO = 1;
Button browseProfilePic;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);//For option menu
View view = inflater.inflate(R.layout.fragment_layout_profilepic, container,
false);
imageView = (ImageView)view.findViewById(R.id.profile_image);
browseProfilePic = (Button) view.findViewById(R.id.btn_pick);
browseProfilePic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PHOTO);
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_PHOTO && resultCode == getActivity().RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater ) {
super.onCreateOptionsMenu(menu, inflater);
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.menu_profile_pic, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()){
case R.id.action_done:
Toast.makeText(getActivity().getApplicationContext(), "Sa", Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
}
MainActivity.java:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
}