1

In the app I am building, the user selects a picture from gallery and the path is saved in Shared Preferences. I then want to retrieve this picture from the stored path but it doesn't work. The image is an ImageButton, which the user clicks in order to select picture from Gallery. The code I have to retrieve the picture and "put" on the ImageButton is:

This code does actually work now, put it here in case it helps others.

       File imgFile = new File(sharedpreferences.getString(Path, LOCATION_SERVICE));
       if(imgFile.exists())
         {
            Bitmap b = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
            ImageButton img=(ImageButton)findViewById(R.id.AddPic);
            img.setImageBitmap(b);
        }

I know the path is correct, but I am not able to retrieve the picture and put it on the ImageButton.

Below is the code where the user clicks on the ImageButton, and selects a picture from the gallery and the path of that picture is stored within the sharedPreferences:

            imgButton = (ImageButton) findViewById(R.id.AddPic);
            imgButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent GaleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(GaleryIntent, RESULT_LOAD_IMAGE);

            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri SelectedImage = data.getData();
            String[] FilePathColumn = {MediaStore.Images.Media.DATA };

            Cursor SelectedCursor = getContentResolver().query(SelectedImage, FilePathColumn, null, null, null);
            SelectedCursor.moveToFirst();

            int columnIndex = SelectedCursor.getColumnIndex(FilePathColumn[0]);
            String picturePath = SelectedCursor.getString(columnIndex);
            SelectedCursor.close();

            imgButton.setImageBitmap(BitmapFactory.decodeFile(picturePath));

            Editor editor = sharedpreferences.edit();
            editor.putString(Path, picturePath);
            editor.commit();

        }

    }   

What am I doing wrong? Thanks very much!

user3079872
  • 191
  • 1
  • 5
  • 15
  • Try [this](http://stackoverflow.com/a/16804467/1777090) way as well. – MysticMagicϡ Sep 02 '14 at 09:54
  • Show your code for Sharedpreference. – Piyush Sep 02 '14 at 10:00
  • You need to access SharedPrefences again with a get() command, then add what you get back to decodeFile. – RED_ Sep 02 '14 at 10:02
  • Try using Uri to get the actual path of the image. – Vikas B L Sep 02 '14 at 10:04
  • @PG_Android, I have added the code where the user selects the picture from the Gallery and the path is saved in Sharedpreferences. I do a Toast and the path is stored correctly, but I am obviously not retrieving it right to show the image again when the activity is started again. – user3079872 Sep 02 '14 at 14:01
  • @RED_ I thought that is what I am doing ... obviously not! What would the code be to use the get() for the shared preferences? – user3079872 Sep 02 '14 at 14:02
  • @user1728071, I use URI to get the file path, and it is stored in the shared preferences, but I don't seem to be able to retrieve the picture using the path – user3079872 Sep 02 '14 at 14:02
  • @Dhruti, thanks for the link, I tried that way, but it still does not retrieve the picture. – user3079872 Sep 02 '14 at 14:03
  • The following code works, not sure I understand, but thank you all, as it is looking at what you suggested that I got it working: ' File imgFile = new File(sharedpreferences.getString(Path, LOCATION_SERVICE)); if(imgFile.exists()) { Bitmap b = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); ImageButton img=(ImageButton)findViewById(R.id.AddPic); img.setImageBitmap(b); }' – user3079872 Sep 02 '14 at 14:41

0 Answers0