How to store and retrieve bitmap in sharedPreferences in Android?
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.