Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 work 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.

like image 307
anddev Avatar asked Sep 06 '11 05:09

anddev


Video Answer


1 Answers

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

like image 118
anddev Avatar answered Oct 06 '22 10:10

anddev