Saving activity state with photos loaded from phone?

401 views Asked by At

I've got an activity that takes photos from your gallery and put them on the screen, but when i exit the app and goes back they wont stay saved there. How do i save them in the activity when i exit and come back? Been trying with onSavedInstanceState but i got no clue what im doing anymore. Im kinda new to this. Here is my activity:

public class HatActivity extends Activity {
private static final int RESULT_LOAD_IMAGE = 1;
LinearLayout main;
Bitmap b;
int count = 0;

@Override
public void onSaveInstanceState(Bundle toSave) {
    super.onSaveInstanceState(toSave);
    toSave.putParcelable("bitmap", b);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (savedInstanceState != null) b = savedInstanceState.getParcelable("bitmap");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hat);
    main = (LinearLayout) findViewById(R.id.main);

    getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getActionBar().setCustomView(R.layout.actionbar);
    getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.abc_textfield_search_right_default_holo_light));
    getActionBar().setStackedBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.abc_search_url_text_normal)));
}

public void infoClick(View view) {
    Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, 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 cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        final ImageView iv = new ImageView(this);
        lparams.gravity=0x11;
        lparams.topMargin=60;
        lparams.bottomMargin=30;
        count++;
        BitmapFactory.Options options=new BitmapFactory.Options();
        options.inSampleSize = 4;
        b = BitmapFactory.decodeFile(picturePath, options);
        iv.setImageBitmap(Bitmap.createScaledBitmap(b, 480, 460, false));
        main.addView(iv, count, lparams);
    }
}

}

1

There are 1 answers

3
Rick On

Looks like your loading the images then tring to savestate the images.

You need to savestate the paths then reload the images. Unless your going to copy the images into a file (gallery) of your creation. Which is bad in my opinion because of the space that would use up.

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Always call the superclass so it can restore the view hierarchy
    super.onRestoreInstanceState(savedInstanceState);

    // Restore state members from saved instance
   mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
   mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
}