共享首选项ImageView

时间:2017-03-15 15:10:29

标签: java local-storage persistent-storage

我在Android工作室工作,我有这个应用程序,显示一个imageView,并有搜索栏,允许您编辑照片的RGB值,但当我从纵向旋转到横向(或反之亦然)时,imageView不会保存并返回原始imageView而不是我选择的那个。 Seekbars保留了他们的价值观。关于如何使用共享首选项来保存图像的任何想法?感谢

公共类MainActivity扩展了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);
}

如果我保存了颜色值(在没有此代码的情况下已经以某种方式保存),这就是它的样子。

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

    }

图片不是Int,那么应该放在哪里? 还得到像RBar = savedInstanceState.getInt这样的确切RGB值,因为Rbar是一个搜索栏而不是一个int ???

0 个答案:

没有答案