Storing image in sharedpreference

3070 views java
-1

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 im new to this and not able to figure it out... I saw some questions related to this but aren't clear. Can someone help me out on how to store path/image in sharedpreference

Code isnt even comoiling as i have put a uri in inout stream in .decodeStream()

answered question

1 Answer

12

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.

posted this

Have an answer?

JD

Please login first before posting an answer.