Image Uri exist but imageview is blank when app restarts

I’m having issues with setting images in a imageview after the app restarts.

I’m using an intent to get the image from an android device;

 // Trigger gallery selection for a photo
public void onPickPhoto() {
    // Create intent for picking a photo from the gallery
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);

    // If you call startActivityForResult() using an intent that no app can handle, your app will crash.
    // So as long as the result is not null, it's safe to use the intent.
    if (intent.resolveActivity(requireActivity().getPackageManager()) != null) {
        // Bring up gallery to select a photo
        startActivityForResult(intent, 0);
    }
}

I then retrieve the uri of the image and set it into an imageview using either Picasso or setImageURI(). I then save the uri in sharedpreferences.

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (data != null && resultCode == RESULT_OK) {
        switch (requestCode) {
            case 0:
                // Load the image located at photoUri into selectedImage
                photoUri = data.getData();

                // Load the selected image into a preview
//                    Picasso.get().load(photoUri).fit().centerCrop().into(accountImageView);
                accountImageView.setImageURI(photoUri);

                //Save imageUri in sharedPreferences
                imagePreferences = requireActivity().getSharedPreferences(SHAREDIMAGE, Context.MODE_PRIVATE);
                editor = imagePreferences.edit();
                editor.putString(IMAGEURI, photoUri.toString());
                editor.apply();
                break;
            case 1:
                photo = (Bitmap) data.getExtras().get("data");
                accountImageView.setImageBitmap(photo);
                break;
        }
    }
}

I’m retrieving the uri in onstart to use but although the uri still exists when I checked with log, the imageview is still blank

 @Override
public void onStart() {
    super.onStart();

    imagePreferences = requireActivity().getSharedPreferences(SHAREDIMAGE,Context.MODE_PRIVATE);
    if(imagePreferences !=null){
        String imageUri = imagePreferences.getString(IMAGEURI,null);
        if(imageUri !=null){
            photoUri = Uri.parse(imageUri);
            // Load the selected image into a preview
//            Picasso.get().load(photoUri).fit().centerCrop().into(accountImageView);
                accountImageView.setImageURI(photoUri);
            }
        }else {
            // Load the selected image into a preview
           accountImageView.setImageResource(R.drawable.battlecruiser);
        }

I’ve tried invalidating the imageview and checking if onstart() is called (and it’s always called) but I’ve seen no changes. What also bugs me is that although I’ve put an image in my drawable to use incase my uri is null, the image is never used and the imageview is blank even when I clear sharedpreferences.

What else should I do?

Answer

Yes you use the same uri but the read and or write permission you got is not valid anymore.

To keep permissions valid after restart of your app you should take persistable uri permissions in onActivityResult.

 takePersistableUriPermission()

Further you should use ACTION_OPEN_DOCUMENT instead of ACTION_GET_CONTENT.