Save Image in Shared Preferences

How to save image in Shared Preferences ? Learn the step-by-step process of saving images in Android Shared Preferences with our comprehensive guide. Discover efficient techniques to store and retrieve images, enhancing your app's performance.

Saving the image:

  • It sets an OnClickListener for the btnSaveImage button.
  • Inside the OnClickListener, it gets the Bitmap from the imageView using imageView.getDrawable().
  • The Bitmap is compressed into a PNG format with 100% quality and stored in a ByteArrayOutputStream.
  • The byte array is obtained from the ByteArrayOutputStream, and then encoded to a Base64 string using Base64.encodeToString().
  • The encoded image string is stored in shared preferences using the key image using the editor object.
  • The changes to shared preferences are applied using editor.apply().
  • A toast message is displayed to indicate that the image has been saved.

Loading the saved image:

  • It retrieves the encoded image data from shared preferences using the key image.
  • The encoded string is then decoded using Base64 decoding to obtain the byte array.
  • The byte array is then decoded into a bitmap using BitmapFactory.decodeByteArray().
  • If the resulting bitmap is not null, it is set as the image for the saveImage ImageView.

Xml code activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:app="http://schemas.android.com/apk/res-auto"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical"  
   tools:context=".MainActivity">  
   
   <ImageView  
     android:id="@+id/imageView"  
     android:layout_width="match_parent"  
     android:layout_height="300dp"  
     android:scaleType="centerCrop"  
     android:src="@drawable/natural_img"  
     />  
   
   <Button  
     android:layout_marginHorizontal="20dp"  
     android:layout_marginTop="10dp"  
     android:id="@+id/btnSaveImage"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:text="Save Image"  
     />  
   
   <ImageView  
     android:layout_marginTop="10dp"  
     android:id="@+id/saveImage"  
     android:layout_width="match_parent"  
     android:layout_height="200dp"  
     />  
   
 </LinearLayout>  

Java Code MainActivity.java

 public class MainActivity extends AppCompatActivity {  
   SharedPreferences preferences;  
   SharedPreferences.Editor editor;  
   
   ImageView imageView,saveImage;  
   Button btnSaveImage;  
   
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
   
     // initialize  
     imageView = findViewById(R.id.imageView);  
     saveImage = findViewById(R.id.saveImage);  
     btnSaveImage = findViewById(R.id.btnSaveImage);  
   
     // SharedPreferences initialize  
     preferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);  
     editor = preferences.edit();  
   
     // get data  
     String encodedSaveImage = preferences.getString("image","");  
     byte[] decodedString = Base64.decode(encodedSaveImage,Base64.DEFAULT);  
     Bitmap decodeBitmap = BitmapFactory.decodeByteArray(decodedString,0,decodedString.length);  
     if (decodeBitmap != null){  
       saveImage.setImageBitmap(decodeBitmap);  
     }  
   
     // save data  
     btnSaveImage.setOnClickListener(v -> {  
       BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();  
       Bitmap bitmap = bitmapDrawable.getBitmap();  
   
       ByteArrayOutputStream outputStream = new ByteArrayOutputStream();  
       bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);  
       byte[] byteArray = outputStream.toByteArray();  
       String encodedImage = Base64.encodeToString(byteArray,Base64.DEFAULT);  
   
       editor.putString("image",encodedImage);  
       editor.apply();  
       Toast.makeText(this, "Image Save", Toast.LENGTH_SHORT).show();  
   
     });  
       
   
   } // onCreate method end here ============  
     
 } // public class end here ===================  

Video: 



Next Post Previous Post
No Comment
Add Comment
comment url