how can I store image in Shared Preferences and retrieve it? - android

I am developing an android application. In my application, I am displaying images from url using xml parsing. I want to store images in device's Shared Preferences. How can I store image in shared preferences and retrieve image from shared preferences? How is it possible?
Thanks All.

I'll try to give you an idea, i use to store temporary object using shared preference, so use kind of serialization, i wrote my own Base64 Encoder/Decorer and basically.
Step 1) Encode your Bitmap or JP..etc
Step 2) Put it in Shared pref as string. (encoded string)
Step 3) Retrive your Encoded object from SharedPref
Step 4) Decode it and you will have your object Back (your pic).
It's not that hard to do it.

Why would you store it in the SharedPreferences? That is not the correct location for storing images.
You should cache them on the external storage if one is available and if not then you should try to store them on the internal storage but watch out when working with the internal storage as you generally won't have as much space as on the external storage.
Check out this article on using the external storage.

Related

How to save Image from ImageView to Android Shared Storage? I dont want my images to delete when user uninstall the app

I want to store my file in android Shared Storage. All the ways I have found store image in internal storage which I don't want because internal storage will be deleted when user uninstall the app.
A code would be helpful which store the image from ImageView to shared storage. I have tried reading android documentation but I don't understand what's written there.
Looking at the table below you should use the MediaStore API for the job. Source: Android data storage overview
Then here's a thorough example how you can write image to file using the MediaStore API.

Saving a file to your own android application

When My application is running I want to capture some information and save this to a folder in my application rather than to the device. I have read online that it is not possible to save a file to your assets or res directory. However would it be possible to save it to another file in your application?I had a look at Shared Preferences but that seems to be only for Key Value pairs.
http://developer.android.com/guide/topics/data/data-storage.html
Writing to internal storage with MODE_PRIVATE should handle what I think you want..
You cannot store the files in the Assets or the Raw folder at run time.
Instead if you have image/video/audio/textual files you can store the files in your SD card and store the path of those files in your file in Database or shared preferences.
After installation each application creates a storage located in '/data/data/com.your.app.package.name'. Save whatever you want in that place.

can we store shared preference on SD CARD

My android application is getting large and we want to store it in SD CARD. can i store shared preference in SD CARD along with my application
You cannot modify where shared preferences are stored. Since shared preferences are simply stored in an XML file, you are welcome to read and write XML data on external storage as you see fit.

Store image into database or in application folder?

Hi i want to store an image in my android application.I am confused that should i store it in the database or in an application folder?which is a better option and y??
Store it on the SDCard if possible else use application folder.
It depends from the type of image you will want to store. It's confidentially? Or public?
Create a database only for saving an image it would be non-optimal, but it you have a database created, for example to save user profiles (name, surname, birthday, etc), it could be a great option to save a profile image in the database.
Anyway, i think sdcard is the best option. And create a custom folder for your application and save all the images there ;)
Hope this helps...
Its depends on the size of images, you want to provide security to that images etc..
If not bother then store images in a application folder /asset , /drawable or external storage (If use external storage then save path of those images in database) and use it directly, To store in database is quite some ugly way, convert it into blob type and then store, re fetching from database convert into byte to bitmap..

How to change the android sharedPreferences save path?

I want to change the android sharedPreferences save path,the sharedPreferences save in /data/data/xxx.xxx.xxx/shared_prefs,i want to change path to /sdcard. how i do?
You cannot modify where shared preferences are stored. Since shared preferences are simply stored in an XML file, you are welcome to read and write XML data on external storage as you see fit.
BTW, never use /sdcard. Use Environment.getExternalStorageDirectory(). /sdcard is not the correct path for over a third of existing Android devices.

Categories

Resources