4

I have an activity that opens another activity to get a camera gallery pic. The picture comes back to my original activity and rest in an imageView. That's working fine. How do I save the image so when the user comes back later, or kills to app the image is still there. I know I am supposed the use Shared Preferences to get the image path and not save the image itself but I just don't know how do that.

Activity A

private ImageView im1;
private String selectedImagePath;
private static final int SELECT_PICTURE = 1;

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
    if (requestCode == SELECT_PICTURE) {
    Uri selectedImageUri = data.getData();
    selectedImagePath = getPath(selectedImageUri);
    System.out.println("Image Path : " + selectedImagePath);
    im1.setImageURI(selectedImageUri);
    }}}
public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
    };
   ((Button)dialogView.findViewById(R.id.button3))
   .setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
    }});

Activity B

    Button send = (Button) findViewById(R.id.send);
    send.setOnClickListener(new OnClickListener() {
 public void onClick(View v) {              
            Intent intent=new Intent();
            setResult(RESULT_OK, intent);
            Bundle bundle=new Bundle();
            bundle.putInt("image",R.id.showImg);
            intent.putExtras(bundle);
            finish();

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
         if (resultCode == RESULT_OK) {
         if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            img.setImageURI(selectedImageUri);
        }}}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
    }
SmulianJulian
  • 801
  • 11
  • 33

4 Answers4

4

Override onPause() method in Activity with an image (to understand why onPause, check life cycle of an Activity diagram here: http://developer.android.com/reference/android/app/Activity.html) like this:

@Override
protected void onPause() {
    SharedPrefrences sp = getSharedPreferences("AppSharedPref", 0); // Open SharedPreferences with name AppSharedPref
    Editor editor = sp.edit();
    editor.putString("ImagePath", selectedImagePath); // Store selectedImagePath with key "ImagePath". This key will be then used to retrieve data.         
    editor.commit();
    super.onPause();
}

It means that whenever this Activity goes into background, the image path will be saved in SharedPreferences with name AppSharedPref - this name can be whatever you like, but you need to use the same one when retrieving data.

Then override onResume() method in the same Activity so that you can retrieve image path when Activity comes to foreground:

@Override
protected void onResume() {
    SharedPreferences sp = getSharedPreferences("AppSharedPref", 0);
    selectedImagePath = settings.getString("ImagePath", "");
    super.onResume();
}

You may also want to play with overriding other methods, like for example onStart() according to diagram, but this I leave to you.

Piotr Chojnacki
  • 6,837
  • 5
  • 34
  • 65
  • I tried to implement this into my code but I must be putting this in the wrong place. Doesnt the onPause then OnResume sections go right after onActivityResult section? – SmulianJulian Jun 28 '13 at 05:46
  • No matter where you actually put this, it just has to be inside `Activity`. Yes, it can be after `onActivityResult()`. What is wrong with that? – Piotr Chojnacki Jun 28 '13 at 05:50
  • I marked this the correct answer because I know it's the right way to things even if I cant get it. I have posted another question using what you gave me. Still cant save, maybe you can help. http://stackoverflow.com/questions/17358820/how-to-save-multiple-images-to-imageview-using-shared-preferences – SmulianJulian Jun 28 '13 at 06:52
  • Thanks. I wrote a comment there for you. – Piotr Chojnacki Jun 28 '13 at 06:55
1

You could use the "selectedImagePath" in passing intents from one Activity to another.

in Activity A.

Intent intent = new Intent(this , Activity.class); intent.putExtra("imagePath", selectedImagePath );

and to get it in Activity B,

String strImagePath = getIntent().getExtras().getString("imagePath");

Prachi
  • 3,584
  • 2
  • 24
  • 39
1
String imagePath = ... ;

SharedPrefrences prefs = getSharedPreferences("application_settings", 0);
Editor editor = prefs.edit();
editor.putString("image_path", imagePath);              
editor.commit();
Aditya Kushwaha
  • 839
  • 6
  • 12
1
SharedPreferences prefs = this.getSharedPreferences(
  "com.example.app", Context.MODE_PRIVATE);

To read preferences:

String path = prefs.getString("key", default value); 

To edit and save preferences

prefs.edit().putString("key", value).commit();
Kamal
  • 1,415
  • 13
  • 24