java – Saving a picture to SharedPreferences


How do I save pictures to SharedPreferences ?


Use the code below to save the image in an internal directory:

private String saveToInternalStorage(Bitmap bitmapImage){
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
         // путь /data/data/yourapp/app_data/imageDir
        File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
        // Создаем imageDir
        File mypath=new File(directory,"profile.jpg");

        FileOutputStream fos = null;
        try {           
            fos = new FileOutputStream(mypath);
       // Используем метод сжатия BitMap объекта для записи в OutputStream
            bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
        } catch (Exception e) {
        } finally {
        return directory.getAbsolutePath();

To read a file from internal memory. Use the code given below

private void loadImageFromStorage(String path)
    try {
        File f=new File(path, "profile.jpg");
        Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
        ImageView img=(ImageView)findViewById(;
    catch (FileNotFoundException e) 

A source

Thus, the picture is also saved in the closed folders of your application in the system, and already in SharedPreferences you can save the path to this picture i.e. what saveToInternalStorage returns.

SharedPreferences intended for storing simple variables of type String , int … It is much more logical and, as I think it is more correct, to store there only the path to the picture, and the picture itself in its original form next to the same settings. "a simple and lightweight mechanism based on key-value pairs for storing primitive application data, most often user settings"

Scroll to Top