共享首选项以保存个人资料图片 [英] shared preference to save profile image

查看:40
本文介绍了共享首选项以保存个人资料图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my app I want to make a profile page with default image profile and allow to user to change it by take a picture from camera or choose image from gallery, I did that successfully and here's my code:

   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 首选项时,我该如何解决这个问题.?

But the problem is when selecting an image I save it in shared preference the image appears just fine when I'am in activity when go to another and return back this disappear and the activity show the default image, i know the reason it because every time i return to profile activity this was created again and the boolean variable d was false again. How can i fix that when i must call the get preference.?

推荐答案

在onActivityResult()"方法中,将获取到的imageUri(或imagePath)保存到sharedPreferences.

In the "onActivityResult()" method, save the imageUri (or imagePath) obtained to sharedPreferences.

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

检查 sharedPreference 值是否不是空字符串,而不是使用布尔值.如果 sharedPreference 值不是空字符串,则使用来自 sharedPreference 的 imageUri(或 imagePath)更新图像视图.

Instead of using boolean, check if sharedPreference value is not a null string.If the sharedPreference value is not a null string, update the imageview with imageUri (or imagePath) from sharedPreference.

要在 imageView 中加载图像,我建议您使用照片加载库,例如 Picasso滑翔.

For loading image in imageView, i recommend you to use photo loading libraries like Picasso or Glide.

这篇关于共享首选项以保存个人资料图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆