user10238140
user10238140

Reputation:

Storing image in SharedPreferences

mHomePage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_PICK);
            intent.setType("image/*");
            startActivityForResult(intent, REQUEST_CODE);
        }
    });
    mHomePage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryIntent, RESULT_LOAD);
        }
    });


    return rootView;
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // When an Image is picked
    if (requestCode == RESULT_LOAD && resultCode == RESULT_OK) {
        Uri resultUri = data.getData();
        CropImage.activity(resultUri)
                .start(getActivity());
    }

    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        Uri uri = result.getUri();

        Bitmap realImage = BitmapFactory.decodeStream(uri);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        realImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();

        String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);

        SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(getActivity());
        SharedPreferences.Editor edit=shre.edit();
        edit.putString("image_data",encodedImage);
        edit.commit();

    }

Trying to store image in sharedpreference by encoding but I'm new to this and not able to figure it out. I saw some questions related to this problem but those aren't clear. Can someone help me out on how to store path/image in SharedPreferences?

Code isn't even compiling as I have put a uri in inout stream in .decodeStream().

Upvotes: 0

Views: 68

Answers (1)

Ezzy
Ezzy

Reputation: 1483

The Uri points to the path where the image is stored, so first you would have to read it with InputStream.

This code will fix the compilation error. As a side-node, use edit.apply() instead of edit.commit().

if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
    try
    {
        InputStream ims = getContentResolver().openInputStream(uri);
        Bitmap realImage = BitmapFactory.decodeStream(uri);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        realImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();

        String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);

        SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor edit=shre.edit();
        edit.putString("image_data",encodedImage);
        //edit.commit();
        edit.apply();
    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
}

However, I don't really see a point in storing an image in SharedPreferences? It's not really meant for that. Why don't you use save the file in context.getFilesDir() and read it from there when you need it? It's better than encoding/decoding it.

Upvotes: 1

Related Questions