0

I used to same the firstName in the SharedPreferences like this:

private String filename = "mySharedString";
private SharedPreferences someData;
someData = getSharedPreferences(filename, 0);
Editor editor = someData.edit();
editor.putString("firstName", phoneNumber);
someData.getString("firstName", "NULL");

It works good. Now I want to save the base 64 of an image that could be 4 MB

Can SharedPreferences be able to save base 64? or that size is large for it ?

user3110137
  • 147
  • 1
  • 10

4 Answers4

4

Don't do this. The file size of 4MB is way to large for SharedPreferences. SharedPreferences were designed to store private primitive data in key-value pairs and surely not for the files. For storing large files use Internal/External Storage and hold Uri to it in the DB.

XorOrNor
  • 8,868
  • 12
  • 48
  • 81
  • I think this question will help me. thank you http://stackoverflow.com/questions/7887078/android-saving-file-to-external-storage – user3110137 Jan 08 '14 at 10:21
  • See this link: http://afterthoughtsoftware.com/posts/Storing-image-URIs-using-Android-SQLite – XorOrNor Jan 08 '14 at 10:26
3

Considering your image is at path --> "/sdcard/test.jpg", see the code below

/*Get image into file*/
File image_file = new File("/sdcard/test.jpg");
/*Get absolute path in bitmap*/
Bitmap mBitmap = BitmapFactory.decodeFile(image_file.getAbsolutePath());
/*Instantiate Byte Array Output Stream to compress the bitmap*/
ByteArrayOutputStream bao_stream = new ByteArrayOutputStream();
/*Compress bitmap*/
mBitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
/*Get byteArray from stream*/
byte[] arr_byte_image = bao_stream.toByteArray();
/*Base6e class has a method name encodeToString using which you can get string from byteArray*/
String img_base64_str = Base64.encodeToString(arr_byte_image, 0);`

Now as we have string containing whole image in base64 string format, we can easily store it into SharedPreferences

Please let me know if there is any doubt in above code.

2

Try this to save in memory

public void savedImage(byte[] data) {
    InputStream in = new ByteArrayInputStream(data);
    BitmapFactory.Options options = new BitmapFactory.Options();
    Bitmap preview_bitmap = BitmapFactory.decodeStream(in, null, options);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    preview_bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
    byte[] byteArray = stream.toByteArray();
    final String newData = Base64.encodeToString(byteArray, 0);
    FileOutputStream outStream = null;
    try {
        outStream = new FileOutputStream(new File(
                Environment.getExternalStorageDirectory(), "ImageName.jpg"));
        outStream.write(byteArray);
        outStream.close();
    } catch (FileNotFoundException e) {
        Log.d("No File", e.getMessage());
    } catch (IOException e) {
        Log.d("IO Error", e.getMessage());
    }
}
Saqib
  • 1,120
  • 5
  • 22
  • 40
  • and the parameter to this function is my base 64 when i changed it to bytes array ? – user3110137 Jan 08 '14 at 10:27
  • just to be clear. the profile image will be saved in `ImageName.jpg` image right? Also, i didn't get you by saying `if it's already then whats the issue? just use the rest of the part.` sorry i am bad in english – user3110137 Jan 08 '14 at 10:31
  • profile image shall be saved with image named imageName.jpg, in your device memory not preference. If your image is already in Base64 you just use the part for saving file, as the answer above takes a byte[] data and converts it to base64, while you already have image in base64, don't u? – Saqib Jan 08 '14 at 10:35
1

Though it's bad idea, but you can do this in such a way

Bitmap photo=BitmapFactory.decodeResource(getResources(), R.drawable.addphoto);

    ByteArrayOutputStream bao = new ByteArrayOutputStream();
                 photo.compress(Bitmap.CompressFormat.JPEG, 100, bao);
                 byte [] ba = bao.toByteArray();
                 String ba1=Base64.encodeBytes(ba);

and store this string ba1 to your `SharedPreferences`

private String filename = "mySharedString";
private SharedPreferences someData;
someData = getSharedPreferences(filename, 0);
Editor editor = someData.edit();
editor.putString("firstName", phoneNumber);
editor.putString("ImageData", ba1);
editor.commit()

I think in this way you will save image in sharedpreferences.

Satyaki Mukherjee
  • 2,857
  • 1
  • 22
  • 26
  • my friend, thank you for answering me. i am not asking about `how` to save image. i am asking about `is sharedpreference able to save base 64 of an image that could be 4MB size` ? – user3110137 Jan 08 '14 at 10:16
  • please see my answer , where I have store this in string format. Though it's 4 MB you can, but it's not wise decision . you can save this image in local database . – Satyaki Mukherjee Jan 08 '14 at 10:20
  • +1 for you. thank you for your help. i found a question, witch is posted in the comments with the above answer, is helpful. thanks a lot. – user3110137 Jan 08 '14 at 10:23
  • 1
    I appreciate you help and support. I hope I can help you one day. best regards – user3110137 Jan 08 '14 at 10:25