user2376920
user2376920

Reputation: 242

Get string from sharedpreference, convert to bitmap, display in imageview

I am converting my image to string and storing that string in sharedpreferences. then later on other activity I want to fetch that string convert back to bitmap and display it in image view. Also for precausion if nothing is fetched from sharedpreference I would like to set ic_launcher as default image in my ImageView.

THis is how i am trying to get above task done.

String pic = shared.getString("UserPic","");
    Log.i("picstring-verifydetail" , "picstring : "+pic);
    if (pic != null && pic != "") {
        try {
            userpic = ImageHelper.stringToImage(pic);
            profilepic.setImageBitmap(userpic);
        } catch (IOException e) {
            Log.e("picsetting", e.toString());
            e.printStackTrace();
        }
    }
    else
    {
        Bitmap defaultImage = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
        profilepic.setImageBitmap(defaultImage);
    }

I have also stored some values like name and that are successfully fetched but string for image is not getting fetched from sharedpreferences. It is always going to else part and there again I am getting error : "Source not found" on profilepic.setImageBitmap(defaultImage);. I searched logcat but found no error.

Please help to achieve these 2 task.

Thanks & Regards,
Sourabh Gupta

Upvotes: 0

Views: 511

Answers (2)

Dulanga
Dulanga

Reputation: 1346

I don't think what you are trying to do is a good idea.

Try to save the image in SD card or Internal Storage and just store the File path in SharedPreferences.

Upvotes: 1

Manjunath
Manjunath

Reputation: 2063

If you have these images stored in assets OR res folders. You can just store the image names into the SharedPreferences and later you can fetch the image names from it and display on the screen by fetching them from the path.

Upvotes: 1

Related Questions