共享首选项以保存配置文件图像

时间:2017-12-27 18:45:48

标签: java android sharedpreferences

在我的应用程序中,我想制作一个带有默认图像配置文件的个人资料页面,并允许用户通过从相机拍摄照片或从图库中选择图像来更改它,我成功完成了这一步,这是我的代码:< / p>

   public class MainActivity extends AppCompatActivity {
    private static final int pick = 1, capture = 2;
    Uri imgeUri, touri;
    ImageView imp;
    SharedPreferences sh;
    SharedPreferences.Editor editor;
    String S;
    boolean d=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("eee","in resume");

        setContentView(R.layout.activity_main);
        sh = getSharedPreferences("my" ,Context.MODE_PRIVATE);
        editor=sh.edit();
        imp = (ImageView) findViewById(R.id.profile_image);

        if(d==false) {
            imp.setImageResource(R.drawable.photo);
        }
        else{
            imp.setImageURI(Uri.parse(sh.getString("link", null)));
        }


}


public void changepic(View V) {
    final String[] items = {"Take picture", "Choose Picture", 
     "cancle"};

    AlertDialog.Builder build = new AlertDialog.Builder(this);
    build.setTitle("Add Photo");
    build.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (items[which].equals("Choose Picture")) {
                Log.d("test","bh");

                Intent i = new Intent(Intent.ACTION_PICK);
                i.setType("image/*");
                startActivityForResult(i, pick);

            } else if (items[which].equals("Take picture")) {

                Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(i, capture);
            }
        }
    }).create().show();

}    


@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == pick && resultCode == RESULT_OK) {

            imgeUri = data.getData();
            Log.d("test","pick");

            imp.setImageURI(imgeUri);

            editor.putString("link",String.valueOf(imgeUri));
            //Log.d("test",f);
            editor.commit();
            d=true;

        } else if (requestCode == capture && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            imp.setImageBitmap(imageBitmap);
        }
    }    

但问题是在选择图像时我将其保存在共享偏好中,当我在活动中时,图像显示正常,当转到另一个并返回时消失,活动显示默认图像,我知道之所以这样,是因为每次我回到个人资料活动时都会再次创建,而布尔变量d又是假的。 当我必须调用get偏好时,我该如何解决?

1 个答案:

答案 0 :(得分:0)

在“onActivityResult()”方法中,将获得的imageUri(或imagePath)保存到sharedPreferences。

data class Job(
  @Expose @SerializedName("type") val type: String, //success
  @Expose @SerializedName("text") val text: String, //200
  @Expose @SerializedName("data") val  data: List<Data>
)

不使用布尔值,而是检查sharedPreference值是否不是空字符串。如果sharedPreference值不是空字符串,请使用sharedPreference中的imageUri(或imagePath)更新imageview。

要在imageView中加载图片,我建议您使用照片加载库,例如PicassoGlide