4

Hey i have been looking for a while now. the following code picks the image from the android gallery and shows it in an imageView. but heres the thing, everytime the app is closed and restarted the has to be picked again. i would like to know how i can edit the following to save the image for good in the imageView.

@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 cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }


}
Aashir
  • 2,621
  • 4
  • 21
  • 37

1 Answers1

5

The only thing that the user is picking is the path of the picture. So if you save the path to SharedPreferences, then everytime the app is started, you can use your existing code, but just change where you get the path:

String picturePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
if(!picturePath.equals(""))
{
   ImageView imageView = (ImageView) findViewById(R.id.imgView);
   imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}

EDIT: This is a complete method you can use in OnCreate:

String picturePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
if(!picturePath.equals(""))
{
   ImageView imageView = (ImageView) findViewById(R.id.imgView);
   imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
else {
   selectImage();
}

In select image use your current code to start the picking activity, then in onActivityResult use this:

@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 cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        PreferenceManager.getDefaultSharedPreferences(this).edit().putString("picturePath", picturePath).commit();
        cursor.close();

        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }
Ryan S
  • 4,549
  • 2
  • 21
  • 33
  • it didnt seem to work. selected the image resulted in an empty imageview – Aashir Sep 07 '13 at 00:41
  • I think you misunderstood, the first time you need to use your original method, but also save the path to shared preferences so that next time you can use my method – Ryan S Sep 07 '13 at 04:45
  • i tried saving to shardpreferences by this: http://pastebin.com/6a8Xt0bx. but the app kept crashing saying that it was unable to deliver path to the activity. is there another way i can save the path? – Aashir Sep 07 '13 at 09:35
  • 1
    thanks that works!. heres the thing i got sharedpreferences working previously, this code works in a seperate project, but when i add it to my own project, the image is not applied reutrning a NPE. i asked here previously but couldnt get any answer maybe you can help out. http://stackoverflow.com/questions/18673235/android-failure-delivering-result-info-on-image-slection – Aashir Sep 07 '13 at 21:23