How to store and retrieve bitmap in sharedPreferences in Android?
I am new in Android. I want to store my bitmap in sharedPreferences. Can anyone tell me how it will possible? Actually my requirements is, I fetch the image from gallery as well as take picture from camera and I set that Bitmap in my ImageView. These all things wo开发者_运维知识库rk properly. But when I click on back button all ImageView will be empty.
So I want to store that Bitmaps throughout my application.
Can anyone help me? I am very much stuck on this.
Thanks.
Hey friends I got the solution of my problem here I post my code so that others can use this solution..
1). on button click - open camera for captureing image
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
2). on button cllick - open Gallery for select image
Intent galleryintent = new Intent(Intent.ACTION_GET_CONTENT);
galleryintent.setType("image/*");
startActivityForResult(galleryintent, IMAGE_PICK);
3). Static variables
private static final int CAMERA_REQUEST = 0;
private static final int IMAGE_PICK = 1;
4). onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch(requestCode)
{
case CAMERA_REQUEST:
if(resultCode == RESULT_OK)
{
String[] projection = { MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null);
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String capturedImageFilePath = cursor.getString(column_index_data);
Log.d("photos*******"," in camera take int "+capturedImageFilePath);
Bitmap photo_camera = BitmapFactory.decodeFile(capturedImageFilePath, options);
if(data != null)
{
img_1.setImageBitmap(photo_camera);
prefsEditor.putString(Global.PHOTO_1,capturedImageFilePath);
}
}
case IMAGE_PICK:
if(resultCode == RESULT_OK)
{
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
// Bitmap photo = BitmapFactory.decodeFile(filePath);
Bitmap photo_gallery = BitmapFactory.decodeFile(filePath,options);
img_1.setImageBitmap(photo_gallery);
prefsEditor.putString(Global.PHOTO_1, filePath);
}
}
prefsEditor.commit();
}
5). in onDestroy() You have to destroy all bitmap which you setted.
@Override
public void onDestroy()
{
super.onDestroy();
if(photo_camera != null)
{
photo_camera.recycle();
}
if(photo_gallery != null)
{
photo_gallery.recycle();
}
}
6). At the time when you fetch data from sharedPrefrences you have to convert string in to Bitmap and then you can set bitmap in ImageView. for example, Bitmap bit1 = BitmapFactory.decodeFile(strimg1); and then set , imageView.setImageBitmap
Don't store bitmaps in a sharedpreference. If you need to persist it during the lifetime of your application, you can assign it to a static
field. If you want to persist it even across device reboots, put it in a file or on the database.
For more information, read http://developer.android.com/resources/faq/framework.html#3
You can add values in SharedPreference
like this:
SharedPreferences pref = getSharedPreferences("abc", 0);
Editor edit = pref.edit();
edit.putBoolean(arg0, arg1);
edit.putFloat(arg0, arg1);
edit.putInt(arg0, arg1);
edit.putLong(arg0, arg1);
edit.putString(arg0, arg1);
edit.commit();
You can add only Boolean, Float, Int, Long, String
values in SharedPreference
.
To store image you should external or internal memory of device.
Let's say the bitmap image as bitmapImg, you can store the bitmap image after converting into a base64String like the below
SharedPreferences mSharedPreferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmapImg.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] compressImage = baos.toByteArray();
String sEncodedImage = Base64.encodeToString(compressImage, Base64.DEFAULT);
mSharedPreferences.edit().putString("keyStoredImage",sEncodedImage);
mSharedPreferences.edit().commit();
And retrieve the Stored Image from the SharedPreference like the below
if(mSharedPreferences.contains("keyStoredImage"))
{
String encodedImage = mSharedPreferences.getString("keyStoredImage",null);
byte[] b = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap bitmapImage = BitmapFactory.decodeByteArray(b, 0, b.length);
}
精彩评论