The image on the ImageView is lost after the action is destroyed

I am trying to create an application where I can let the user select an image to display in their profile. I can view and set their selected image in image view mode. But the image is lost after the destruction of activity. I tried to implement onSaveInstanceState, but still it's the same. I am wondering if I am using it correctly. Hope you can help newbies like me. Thanks in advance. Here is the code I'm using:

public class AccountFragment extends Fragment implements OnClickListener { private LoginDataBaseAdapter loginDataBaseAdapter; Bitmap image; Bitmap bitmap; String picture_location; TextView textTargetUri; ImageView targetImage; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_account, container, false); textTargetUri = (TextView) rootView.findViewById(R.id.targeturi); targetImage=(ImageView) rootView.findViewById(R.id.profpic); targetImage.setOnClickListener(new ImageView.OnClickListener(){ @Override public void onClick(View arg0) { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, 0); }}); if (savedInstanceState != null) { //if there is a bundle, use the saved image resource (if one is there) image = savedInstanceState.getParcelable("BitmapImage"); targetImage.setImageBitmap(image); textTargetUri.setText(savedInstanceState.getString("path_to_picture")); } return rootView; } @Override public void onSaveInstanceState(Bundle savedInstanceState){ super.onSaveInstanceState(savedInstanceState); savedInstanceState.putParcelable("BitmapImage", bitmap); savedInstanceState.putString("path_to_picture", picture_location); } @Override public void onActivityResult( int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == Activity.RESULT_OK){ Uri targetUri = data.getData(); picture_location = targetUri.toString(); textTargetUri.setText(targetUri.toString()); Bitmap bitmap; try { bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(targetUri)); targetImage.setImageBitmap(bitmap); } catch (FileNotFoundException e){ e.printStackTrace(); } } } @Override public void onClick(View arg0) { // TODO Auto-generated method stub }} 

By the way, you may have noticed that instead of using onRestoreInstanceState after oncreate, I tried to use a different approach. I found the answer from another question that you can also implement in oncreate. I have used it since whenever I declare an onRestoreInstanceState function, I am asked to remove the @Override annotation.

 @Override protected void onRestoreInstanceState(Bundle savedInstanceState){ image = savedInstanceState.getParcelable("BitmapImage"); targetImage.setImageBitmap(image); textTargetUri.setText(savedInstanceState.getString("path_to_picture")); } 
+1
android android-fragments imageview
source share
3 answers

Using onSaveInstanceState and onCreate / onRestoreInstanceState is intended to maintain a state of short-term activity, but not to permanently store application data.

You can read about onSaveInstanceState here.

You can read about persistent storage here.

codeMagic suggested using SharedPrefs (see permalink for storage) for your long-term persistent storage. If you want to do this, I would suggest saving the URI of the image (the link has a good example of how to do this) in your onActivityResult method, and then calling the SharedPref method to read and loading the image that you can call from onCreate, as well as fromActivityResult.

You can also save your own copy of the image / bitmap in your own internal storage of the application (see the link to the persistent storage).

+1
source share

if you haven't finished the action, you can use onSavedInstance() to store the picture_location value and bind it back to onCreate(SavedInst) / onRestore() from the picture_location value.

+1
source share

In the case of state of Bitmaps instances, there is no way to save information about the selected image.

Here you can find an explanation: Handling configuration changes

I talked about this in detail here: Save the selected image while rotating the screen

Below I insert my implementation of the illustrated solution:

1 - Create a fragment and configure it to save in memory

 import android.graphics.Bitmap; import android.os.Bundle; import android.support.v4.app.Fragment; public class ImageRetainingFragment extends Fragment { private Bitmap selectedImage; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // retain this fragment setRetainInstance(true); } public void setImage(Bitmap selectedImage) { this.selectedImage = selectedImage; } public Bitmap getImage() { return this.selectedImage; } } 

2 - use it in your activity

 private static final String FRAGMENT_NAME = "imageFragment"; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); .... initializeImageRetainingFragment(); tryLoadImage(); } private void initializeImageRetainingFragment() { // find the retained fragment on activity restarts FragmentManager fragmentManager = getSupportFragmentManager(); this.imageRetainingFragment = (ImageRetainingFragment) fragmentManager.findFragmentByTag(FRAGMENT_NAME); // create the fragment and bitmap the first time if (this.imageRetainingFragment == null) { this.imageRetainingFragment = new ImageRetainingFragment(); fragmentManager.beginTransaction() // Add a fragment to the activity state. .add(this.imageRetainingFragment, FRAGMENT_NAME) .commit(); } } private void tryLoadImage() { if (this.imageRetainingFragment == null) { return; } Bitmap selectedImage = this.imageRetainingFragment.getImage(); if (selectedImage == null) { return; } ImageView selectedImageView = (ImageView)findViewById(R.id.selectedImage); selectedImageView.setImageBitmap(selectedImage); } 
0
source share

All Articles