There is a fragment from ImageView, I want to upload a photo from the gallery to this ImageView. From the activit works without problems, but from the fragment does not want. Tell me what's the problem?

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_profile, container, false); fp_imageView1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, SELECTED_PICTURE); } }); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Log.e(TAG,String.valueOf(resultCode)); if (requestCode == SELECTED_PICTURE && resultCode == getActivity().RESULT_OK && null != data) { Uri selectedImage = data.getData(); uri = selectedImage.toString(); Picasso.with(getActivity()).load(uri).transform(new CircleTransform()).into(fp_imageView1); } } 

The photo is loaded and disappears immediately.

  • Try to remove the super from the result - YuriySPb
  • @YurySPb tried it - the result is the same! I found a solution, but it is from the category "keep in preference, pull it out of activity", a rake. - Ivan Vovk

1 answer 1

 public class ImageSelector extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View view = inflater.inflate(R.layout.createevent,container,false); imgcover = (ImageView) view.findViewById(R.id.newcover_img); btnupload = (Button) view.findViewById(R.id.newcover_upload); btnupload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, RESULT_LOAD_IMAGE); } }); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { Uri selectedImg = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = getContentResolver().query(selectedImg, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String picturePath = cursor.getString(columnIndex); imgcover = (ImageView) findViewById(R.id.newcover_img); imgcover .setImageBitmap(BitmapFactory.decodeFile(picturePath)); cursor.close(); } } 
  • The fact is that it doesn't work that way. My code is almost the same as yours! The only difference is that you have a listener on the button, and I have an ImageView on it. - Ivan Vovk