0

I'm working in android studios and I have this app that displays an imageView and has seekbars that allow you to edit the RGB values of the photo, but when I rotate from portrait to landscape (or vice versa) the imageView doesnt save and goes back to the original imageView and not the one that I selected. The Seekbars keep their values though. Any ideas on how I can used shared preferences to save the image? Thanks

public class MainActivity extends AppCompatActivity {

////////////////////////////////////////////////////////////////////////////

TextView textTitle;
ImageView image;
SeekBar barR, barG, barB, barAlpha;
int ColorValues;

private static final int SELECT_PHOTO = 100;
private static final int CAMERA_PIC_REQUEST = 101;
private static final String PREFS_NAME = "PrefsFile";
private static final String COLOR_VALUES = "ColorVals";
private static final String PICTURE = "Picture";

///////////////////////////////////////////////////////////////////////////


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    textTitle = (TextView)findViewById(R.id.title);
    image = (ImageView)findViewById(R.id.image);
    barR = (SeekBar)findViewById(R.id.red);
    barG = (SeekBar)findViewById(R.id.green);
    barB = (SeekBar)findViewById(R.id.blue);
    barAlpha = (SeekBar)findViewById(R.id.alpha);

    barR.setOnSeekBarChangeListener(myOnSeekBarChangeListener);
    barG.setOnSeekBarChangeListener(myOnSeekBarChangeListener);
    barB.setOnSeekBarChangeListener(myOnSeekBarChangeListener);
    barAlpha.setOnSeekBarChangeListener(myOnSeekBarChangeListener);

    //default color
    ColorValues = barAlpha.getProgress() * 0x1000000
            + barR.getProgress() * 0x10000
            + barG.getProgress() * 0x100
            + barB.getProgress();


    image.setColorFilter(ColorValues, Mode.MULTIPLY);

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    ColorValues = settings.getInt(COLOR_VALUES, ColorValues);
}

//menu/////////////////

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_menu, menu);
    return true;
}


public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.savePicture:
            Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
            MediaStore.Images.Media.insertImage
                    (getContentResolver(), bitmap, "yourTitle", "yourDescription");

    }
    return true;
}

//on click function////////////////
public void selectPicture(View view) {
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}


public void takePicture(View view){

    try{
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
    } catch(Exception e){
        e.printStackTrace();
    }
}

//activity///////////////
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case SELECT_PHOTO:
            if (resultCode == RESULT_OK) {
                Uri selectedImage = data.getData();
                InputStream imageStream = null;
                try {
                    imageStream = getContentResolver().openInputStream(selectedImage);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                image.setImageURI(selectedImage);// To display selected image in image view
                break;
            }
        case CAMERA_PIC_REQUEST:
            try {
                Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                thumbnail = Bitmap.createScaledBitmap(thumbnail, 700, 500, true);
                ImageView imageView = (ImageView) findViewById(R.id.image);
                image.setImageBitmap(thumbnail);
            } catch (Exception ee) {
                ee.printStackTrace();
            }
            break;
    }
}
//warning before exit

@Override
public void onBackPressed() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle("Closing Activity")
            .setMessage("Are you sure you want to close\nthis activity? \n\nProgress will NOT be saved ")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }

            })
            .setNegativeButton("No", null);
        AlertDialog dialog = builder.show();
        TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
        messageText.setGravity(Gravity.CENTER);
        dialog.show();
}

//edit picture
OnSeekBarChangeListener myOnSeekBarChangeListener = new OnSeekBarChangeListener(){

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
                                  boolean fromUser) {

        //Colorize ImageView
        int newColor = barAlpha.getProgress() * 0x1000000
                + barR.getProgress() * 0x10000
                + barG.getProgress() * 0x100
                + barB.getProgress();
        image.setColorFilter(newColor, Mode.MULTIPLY);

        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        ColorValues = newColor;

        editor.putInt(COLOR_VALUES, ColorValues);
        // Commit the edits
        editor.commit();
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {}

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {}
};



@Override
protected void onSaveInstanceState(Bundle icicle) {
    super.onSaveInstanceState(icicle);

    icicle.putInt(COLOR_VALUES, ColorValues);
}

this is what it would look like if I saved the colorvalues(which are somehow already being saved without this code)

if (savedInstanceState != null) {
        colorvalues = savedInstanceState.getInt("COLOR_VALUES");
        image = savedInstanceState.get??????????????????????

    }

image is not an Int so what values should be put there? also to get the exact RGB values like RBar = savedInstanceState.getInt doesnt works becuase Rbar is a seekbar not an int???

sabby918
  • 1
  • 2
  • usually you store all view relevant data at onSaveInstanceState (it lookslike you dont store the image, only the colorvalues), and set the values from the savestate in onCreate (you are useing default colors only, not from bundle) – Fusselchen Mar 15 '17 at 15:18
  • Im confused how the values are saved (seekbar) if im not using the onSavedInstanceState in my onCreate. I believe you are wanting me to do something like this.... ... if (savedInstanceState != null) { //values } in the onCreate – sabby918 Mar 15 '17 at 15:55
  • right, they are not null after onSaveInstanceState, then you can read the previous colorvalues from that bundle. and you have to do the same for the image. – Fusselchen Mar 15 '17 at 19:00
  • my edit show what the onSaveInt would look like, but apparently i do not need it because the values are already being saved. the real question is how i save the image because the image is not an int so what do i "get" – sabby918 Mar 16 '17 at 20:00
  • this you can read at http://stackoverflow.com/questions/4352172/how-do-you-pass-images-bitmaps-between-android-activities-using-bundles just use `putParcelable(java.lang.String, android.os.Parcelable) ` – Fusselchen Mar 17 '17 at 09:57

0 Answers0