0

I have an activity where a user can upload a photo. At first, before he chooses a photo there is a default image from the drawable folder. But, since I have shared preferences on that view, in case the user goes back to previous activities, I clean the shared preferences in the onDestroy of the main activity, and then when I launch my app again, the image view is blank, instead of displaying the default image. This is how I save the shared preferences:

ActivityTwo.java;

public void onBackPressed() {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString("imageUri", imageURI);
    editor.apply();
    super.onBackPressed();
} 

On the main activity I clean this preference:

@Override
public void onDestroy()
{
    super.onDestroy();
    SharedPreferences preferences = getSharedPreferences("Mypref", 0);
    preferences.edit().remove("imageUri").commit();
}

But then when I launch the app again, the image view at the ActivityTwo is just a blank square, without the default image inside.

So, am I doing it right? The way I clear the shared preferences? Why do I have to do it via the onDestroy of the main activity and not ActivityTwo?(It didnt work). And how to save the default image view?

This is the xml of the image view:

<ImageView
android:id="@+id/imageView"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="40dp"
android:background="@drawable/image_view_border"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/no_profile_picture" />
6
  • 2
    Can you show me the code where you load an image?
    – Zun
    Nov 26, 2018 at 11:26
  • post your xml or show how are you setting the default image drawable
    – nimi0112
    Nov 26, 2018 at 11:52
  • added the xml, thanks
    – pileup
    Nov 26, 2018 at 12:08
  • why are you storing ImgUri into Shared Preferences? Nov 26, 2018 at 12:15
  • what do you want to accomplish?
    – nimi0112
    Nov 26, 2018 at 12:23

2 Answers 2

0

check that the imageuri is null or not, then set image...

if(imageURI!=null)
imageView.setImageURI(imageURI)
else imageView.setImageResource(R.drawable.no_profile_picture);
0

on the activity where you set the default image in the first place, check whether the preference for the custom image is there, if yes, replace the image, if not, use the default.

For example:

finalUri = imageUri == null? defaultUri : imageUri;
imageView.setImageURI(finalUri);

EDIT: to get the drawable reference you can use the following code:

Drawable myImage = getResources().getDrawable(R.drawable.myImage);
4
  • but the default image is in the drawable folder using xml
    – pileup
    Nov 26, 2018 at 12:09
  • you can get the reference to a drawable like so: Drawable myImage = getResources().getDrawable(R.drawable.myImage); Nov 26, 2018 at 12:57
  • But then defaultUri should be of type Drawable, no? then how can it work if you wrote defaultUri?
    – pileup
    Nov 26, 2018 at 13:59
  • check how to get Uri from drawable here: stackoverflow.com/questions/19566840/… Nov 26, 2018 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.