How to save Image in shared preference in Android | Shared preference issue in Android with Image

前端 未结 4 648
难免孤独
难免孤独 2020-11-27 17:34

In my application after login I have to save user name and image in shared preference for other pages. I am able to save name in preference but can\'t get any where how to s

4条回答
  •  生来不讨喜
    2020-11-27 17:56

    Encode to Base64?! That's crazy talk! That's way too much information that you are storing to the shared preferences. The strategy you should be doing is saving the Image URI file path, and retrieving it as such. This way, your app won't be storing so much information and become a memory hog when decoding the image.

    I made a simple App on Github to demonstrate this idea, if you want to follow:

    1. Declare the variables:

    private ImageView mImage;
    private Uri mImageUri;
    

    2. Select the image:

    public void imageSelect() {
         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"),
             PICK_IMAGE_REQUEST);
     }
    

    3. Save the image URI:

    @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         // Check which request we're responding to
         if (requestCode == PICK_IMAGE_REQUEST) {
             // Make sure the request was successful
             if (resultCode == RESULT_OK) {
                 // The user picked a image.
                 // The Intent's data Uri identifies which item was selected.
                if (data != null) {
    
                    // This is the key line item, URI specifies the name of the data
                    mImageUri = data.getData();
    
                    // Removes Uri Permission so that when you restart the device, it will be allowed to reload. 
                    this.grantUriPermission(this.getPackageName(), mImageUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    final int takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION;
                    this.getContentResolver().takePersistableUriPermission(mImageUri, takeFlags);
    
                    // Saves image URI as string to Default Shared Preferences
                    SharedPreferences preferences = 
                         PreferenceManager.getDefaultSharedPreferences(this);
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putString("image", String.valueOf(mImageUri));
                    editor.commit();
    
                    // Sets the ImageView with the Image URI
                    mImage.setImageURI(mImageUri);
                    mImage.invalidate();
                 }
             }
         }
     }
    

    4. Retrieve the image URI when needed:

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String mImageUri = preferences.getString("image", null);
    mImage.setImageURI(Uri.parse(mImageUri));
    

    That's it! Now we have saved the image uri path to shared preferences cleanly, and have not wasted valuable system resources encoding the image and saving it to SharedPreferences.

提交回复
热议问题