0

I'm trying to store a photo in SharedPreferences that it took from inside my app. but i cant find any code that will help me with that. can you help me with it please ?

public class RegisterActivity extends Activity {

    private final int REQUEST_IMAGE = 100;

    EditText nameEditText;
    EditText phoneEditText;
    EditText emailEditText;
    EditText bdayEditText;
    ImageView photoImageView;
    ImageButton cameraButton;
    Button saveButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        // String nameEt = nameEditText.getText().toString();

        // String phoneEt = phoneEditText.getText().toString();

        // String emailEt = emailEditText.getText().toString();

        // String bdayEt = bdayEditText.getText().toString();

        photoImageView = (ImageView) findViewById(R.id.photoImageView);

        cameraButton = (ImageButton) findViewById(R.id.cameraImageButton);
        cameraButton.setOnClickListener(cameraButtonListener);

        saveButton = (Button) findViewById(R.id.saveButton);
        saveButton.setOnClickListener(saveButtonListener);

    }

    private OnClickListener cameraButtonListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            startActivityForResult(intent, REQUEST_IMAGE);

        }
    };

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK) {
            Bitmap userImage = (Bitmap) data.getExtras().get("data");

            photoImageView.setImageBitmap(userImage);
        }
    };

    private OnClickListener saveButtonListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            SharedPreferences sharedPreferences = getSharedPreferences(
                    "WaiterData", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();

            nameEditText = (EditText) findViewById(R.id.nameEditText);
            phoneEditText = (EditText) findViewById(R.id.phoneEditText);
            emailEditText = (EditText) findViewById(R.id.emailEditText);
            bdayEditText = (EditText) findViewById(R.id.bdayEditText);

            if (nameEditText.getText().length() > 0
                    && phoneEditText.getText().length() > 0
                    && emailEditText.getText().length() > 0
                    && bdayEditText.getText().length() > 0) {

                editor.putString("name", nameEditText.getText().toString());
                editor.putString("phone", phoneEditText.getText().toString());
                editor.putString("email", emailEditText.getText().toString());
                editor.putString("bday", bdayEditText.getText().toString());

                // move back to MainActivity
                Intent intent = new Intent(RegisterActivity.this,
                        MainActivity.class);
                startActivity(intent);

            } else {
                Toast.makeText(RegisterActivity.this,
                        "Please fill all The fields", Toast.LENGTH_SHORT)
                        .show();

            }

        }
    };

}

and how can i can get the code out in other activity ?

Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118

1 Answers1

0

The way I'm using to store images using SharedPreferences is to encode your image bitmap to a String then store that string, and then when needing the image retrieve that String and decode it again to a bitmap:

// declaring the sharedPreferences
SharedPreferences sharedPreferences = PreferenceManager
                    .getDefaultSharedPreferences(this);
// encoding image bitmap to String
int size = userImage.getWidth() * userImage.getHeight();
ByteArrayOutputStream stream = new ByteArrayOutputStream(
                                    size);
userImage.compress(Bitmap.CompressFormat.JPEG, 50,
                                stream);
byte[] b = stream.toByteArray();
Editor editor = sharedPreferences.edit();
editor.putString("userImage", encodedImage);
editor.commit();
stream.close();
stream = null;

// then when needing that image again from the SharedPreferences
String decoded_image = sharedPreferences.getString("userImage", "default");
try{
    byte[] decodedString = Base64.decode(decoded_image, Base64.DEFAULT);
    Bitmap stored_userImage = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
    // do whatever you want with it now
}catch(OutOfMemoryError oom){

}
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118