0

I have images selected from the gallery displayed in ImageView, But not retaining or saved Image when the android device restart I need to re-pick an image again. My plan is for the image to still stay on the image view even the device is rebooted , or do I need to create some data to save the image and display to ImageView

public class FirstFragment extends Fragment implements View.OnClickListener{
    ImageView imageButton1;
   
    private Uri mImageUri;
 

    @Override
    public void onResume() {
        super.onResume();
    }

    private File mSnapFile;

    private static final String ARG_URI_IMAGE_1 = "image1Uri";
  

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v= inflater.inflate(R.layout.fragment_first, container, false);
        imageButton1 = (ImageView) v.findViewById(R.id.firstimagebtn);
 
        imageButton1.setOnClickListener(this::onClick);

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String mImageUri = preferences.getString("image", null);
 
        if (mImageUri != null) {
            imageButton1.setImageURI(Uri.parse(mImageUri));
        } 
        return v;
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.firstimagebtn:
                Intent intent;
                if (Build.VERSION.SDK_INT < 19) {
                    intent = new Intent(Intent.ACTION_GET_CONTENT);
                } else {
                    intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                    intent.addCategory(Intent.CATEGORY_OPENABLE);
                }
                intent.setType("image/*");
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), 0);
                break;
           
        }

    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch(requestCode) {
            case 0:
                if(resultCode == Activity.RESULT_OK){
                    if (data != null) {
                        // This is the key line item, URI specifies the name of the data
                        mImageUri = data.getData();
                        // Saves image URI as string to Default Shared Preferences
                        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.putString("image", String.valueOf(mImageUri));
                        editor.commit();
                        // Sets the ImageView with the Image URI
                        imageButton1.setImageURI(mImageUri);
                        imageButton1.invalidate();
                    }
                }
                break;
            case 1:
                if(resultCode == Activity.RESULT_OK){
                    // This is the key line item, URI specifies the name of the data
                    mImageUri2 = data.getData();
                    // Saves image URI as string to Default Shared Preferences
                    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putString("image2", String.valueOf(mImageUri2));
                    editor.commit();
                    // Sets the ImageView with the Image URI
                    imageButton2.setImageURI(mImageUri2);
                    imageButton2.invalidate();
                }
                break;
        }
    }
James
  • 235
  • 1
  • 2
  • 10

1 Answers1

0

You can use image loading libraries like Glide or Picasso. They provide data caching and your image will be persisted.

See docs

By default, Glide checks multiple layers of caches before starting a new request for an image:

1.Active resources - Is this image displayed in another View right now?

2.Memory cache - Was this image recently loaded and still in memory?

3.Resource - Has this image been decoded, transformed, and written to the disk cache before?

4.Data - Was the data this image was obtained from written to the disk cache before?

The first two steps check to see if the resource is in memory and if so, return the image immediately. The second two steps check to see if the image is on disk and return quickly, but asynchronously.

If all four steps fail to find the image, then Glide will go back to the original source to retrieve the data (the original File, Uri, Url etc).

Hope this helps. Let me know if you face an issue with this but I think the docs are pretty much self explanatory.

Om Gupta
  • 192
  • 2
  • 8
  • Is it possible to save the image path to SQLlite then retreieve ? – James Oct 23 '20 at 01:11
  • 1
    Yes it is possible to do that but it is a more tedious approach and is also vulnerable to bugs as you have to implement it from scratch, [as answered in this question](https://stackoverflow.com/questions/9357668/how-to-store-image-in-sqlite-database). Libraries like Gilde and Picasso have all this (and more) implemented so they are safer and easier to use. – Om Gupta Oct 25 '20 at 15:23
  • cool i already make some sratch app, then i see some bugs when android 10 and 11. – James Oct 26 '20 at 00:30