Preservation of images in SharedPreferences
-
How to keep pictures
SharedPreferences
?
-
Use the code shown below to keep the image in the inner 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) { e.printStackTrace(); } finally { fos.close(); } return directory.getAbsolutePath(); }
To read the file from inside memory. Use the code 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(R.id.imgPicker);
img.setImageBitmap(b);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
https://stackoverflow.com/a/17674787/5698593
So the picture is also kept in the closed files of your application in the system, and already in the system.
SharedPreferences
You can keep the way to that picture, that's what's coming back.saveToInternalStorage
♪SharedPreferences
designed to store simple variablesString
♪int
... It's much more logical and, as I think it's better to keep it only on the way to the picture, and the picture itself is in its original form next to the same settings. A simple and light mechanism based on the " key " , designed to preserve the primitive data of the application, most commonly user lines.