简体   繁体   中英

Android-Keep image in ImageView on a fragment when change fragment

I have the code below. I set an image to ImageView from gallery in the fragment. The problem is when i change fragment and get back again to the fragment with the ImageView, it has the default image, not the one i set.

public class Fragment_user extends Fragment {

 private static final int SELECT_PICTURE = 1;
 private String selectedImagePath=null;
 private ImageView img;

@Override
public View onCreateView(
        LayoutInflater inflater,
        ViewGroup container,
        Bundle savedInstanceState){
    View rootView = inflater.inflate(R.layout.fragment_user, container, false);
    //if (savedInstanceState == null){
        img = (ImageView)rootView.findViewById(R.id.userImage);
    //}else{
        //Bitmap bitmap = (Bitmap) savedInstanceState.getParcelable("bitmap");
        //img.setImageBitmap(bitmap);
    //}


            img.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
                }
            });     
    return rootView;
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == MainActivity.RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            img.setImageURI(selectedImageUri);
        }
    }
}

public String getPath(Uri uri) 
{
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null, null);
    if (cursor == null) return null;
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    String s=cursor.getString(column_index);
    cursor.close();
    return s;
}

@Override
public void onSaveInstanceState(Bundle outState) {        
    super.onSaveInstanceState(outState);
    // Save the image bitmap into outState
    Bitmap bitmap = ((BitmapDrawable)img.getDrawable()).getBitmap();
    outState.putParcelable("bitmap", bitmap);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // Read the bitmap from the savedInstanceState and set it to the ImageView
    if (savedInstanceState != null){
        Bitmap bitmap = (Bitmap) savedInstanceState.getParcelable("bitmap");
        img.setImageBitmap(bitmap);
     }  
}

}

I have the onSaveInstanceState method to save the selected image to a bitmap and the onActivityCreated method to set back the image to the ImageView when i get back to Fragment. But this doesnt work. Any help? Thanks in advance

It occurs because your fragment loses its view according to Fragments Lifecycle . To solve your problem make next actions:

  1. Create global variable private Uri mSelectedImageUri ;

  2. Use this variable instead your local Uri selectedImageUri :

     if (requestCode == SELECT_PICTURE) { mSelectedImageUri = data.getData(); selectedImagePath = getPath(mSelectedImageUri); System.out.println("Image Path : " + selectedImagePath); img.setImageURI(mSelectedImageUri); } 
  3. Add check below this line img = (ImageView)rootView.findViewById(R.id.userImage); :

     if (mSelectedImageUri != null) { img.setImageURI(mSelectedImageUri); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM