Search code examples
androidandroid-dialogfragment

Saving DialogFragment Drawable image in Saved preferences


I am trying to load image if user has load an image previously. I am trying to do this in onCreate

Used DialogFragment to achieve this since I want to pick image from within app only.

Here is my WithNamesFragment Class :

public class WithNamesFragment extends DialogFragment {
    final static ArrayList<HashMap<String, ?>> imagesWithNames = new ArrayList<>();

    static {
        HashMap<String, Object> row = new HashMap<>();
        row.put("imageID", R.drawable.aquarius);
        row.put("name", "Aquarius");
        imagesWithNames.add(row);
        row = new HashMap<>();
        row.put("imageID", R.drawable.virgo);
        row.put("name", "Virgo");
        imagesWithNames.add(row);

    }

    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final SimpleAdapter adapter = new SimpleAdapter(getContext(), imagesWithNames, R.layout.lib_dialog_image,
                new String[]{"name", "imageID"}, new int[]{R.id.text1, R.id.image1});
        return new AlertDialog.Builder(getContext()).setAdapter(adapter,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        ((MainActivity) getContext()).OnSelected(WithNamesFragment.this.getClass(), (int) ((HashMap<String, Object>) adapter.getItem(i)).get("imageID"));
                    }
                }).setCancelable(true).setTitle("PICK YOUR AVATAR").create();
    }
}

in MainActivity :

public void OnSelected(Class<?> clazz, int i) {
        Toast toast = Toast.makeText(this, "Image Selected", Toast.LENGTH_SHORT);
        //Toast toast = Toast.makeText(this, "Class \"" + clazz.getSimpleName() + "\" returns this image with id: " + getResources().getResourceName(i), Toast.LENGTH_SHORT);
        final TextView toastView = (TextView) toast.getView().findViewById(android.R.id.message);
        toastView.setCompoundDrawablesWithIntrinsicBounds(0, i, 0, 0);
        toast.show();
        Glide.with(this).load(i)
                .crossFade()
                .thumbnail(0.5f)
                .bitmapTransform(new CircleTransform(this))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(profimg);
        sp.edit().remove("dp").apply();
        i=this.i;
        sp.edit().putInt("dp",i);
    }
    void show() {
        new WithNamesFragment().show(getSupportFragmentManager(), PICKER_TAG);
    }

in Oncreate of MainActivity :

profimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                show();
            }
        });

 boolean path =sp.contains("dp");

if(path) {
           int value=sp.getInt("dp",i);
            Toast.makeText(MainActivity.this, value, Toast.LENGTH_SHORT).show();
            /*Glide.with(this).load(value)
                    .crossFade()
                    .thumbnail(0.5f)
                    .bitmapTransform(new CircleTransform(this))
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(profimg);*/
        }

This is show method:

void show() {
        new WithNamesFragment().show(getSupportFragmentManager(), PICKER_TAG);
    }

Earlier I was struggling with Casting error but now I am getting nothing in Toast.


Solution

  • Replace below line :

     sp.edit().putInt("dp",i).apply();
    

    You have missed the .apply() or .commit() Edit Code : initialize SharedPreference first.

        Oncreate(){
    
            final int value = sp.getInt("dp",0);
    
        if(value>0){
           imageView.setImageResource(value);
        }
    
    
    }