【问题标题】:shared preference to save profile image共享偏好以保存个人资料图像
【发布时间】:2017-12-27 18:45:48
【问题描述】:

在我的应用程序中,我想使用默认图像配置文件创建一个配置文件页面,并允许用户通过从相机拍照或从图库中选择图像来更改它,我成功地做到了,这是我的代码:

   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 再次为假。 当我必须调用获取首选项时,我该如何解决这个问题。?

【问题讨论】:

    标签: java android sharedpreferences


    【解决方案1】:

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

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
    SharedPreferences.Editor edit = preferences.edit();
    edit.putString(key, value);
    edit.apply();
    

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

    对于在 imageView 中加载图像,我建议您使用像 PicassoGlide 这样的照片加载库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多