Reputation: 147
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 ?
Upvotes: 0
Views: 1713
Reputation: 81
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.
Upvotes: 3
Reputation: 1129
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());
}
}
Upvotes: 2
Reputation: 2879
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.
Upvotes: 1
Reputation: 8978
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.
Upvotes: 4