上传您自己的个人资料照片

问题描述

启动应用程序时,我使用Picasso将图像上传到ima​​geview。我正在尝试使用户上传自己的图像。如果我拍照或从画廊中选择它,则可以在imageview中显示它。但是我想在下次加载应用程序时使用SharedPreferences来显示图像,不幸的是我不能这样做。 这是我的代码

private void showImageOptionDialog(){
        final String[] options = getResources().getStringArray(R.array.image_options);
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle(R.string.alert_dialog_title)
                .setItems(options,new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog,int which) {

                        switch (which){
                            case 0:
                                Intent cameraIntent = new Intent();
                                cameraIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                                startActivityForResult(cameraIntent,11);
                                break;
                            case 1:
                                Intent intent = new Intent();
                                intent.setAction(Intent.ACTION_GET_CONTENT);
                                intent.setType("image/*");
                                startActivityForResult(intent,9);
                                break;
                            case 2:
                                SaveSharedPreference.setAvatar(context,"none");
                                recreate();
                                break;
                        }
                    }
                });

        AlertDialog dialog = builder.create();
        dialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode,@Nullable Intent data) {
        super.onActivityResult(requestCode,resultCode,data);

        //Check if the intent was to pick image,was successful and an image was picked
        if(requestCode == 9 && resultCode == RESULT_OK && data != null){

            //Get selected image uri from phone gallery
            Uri selectedImage = data.getData();

            //display selected photo in image view
            //head_image.setimageURI(selectedImage);
            assert selectedImage != null;
            SaveSharedPreference.setAvatar(context,selectedImage.toString());
        }
        //Handle camera request
        else if(requestCode == 11 && resultCode == RESULT_OK && data != null){

            //We need a bitmap variable to store the photo
            Bitmap bitmap = (Bitmap) Objects.requireNonNull(data.getExtras()).get("data");

            //display taken picture in image view
            head_image.setimageBitmap(bitmap);
        }
        recreate();
    }

然后我尝试使用加载

Picasso.get()
                .load(SaveSharedPreference.getAvatar(context))
                .placeholder(R.mipmap.ic_launcher)
                .transform(new CropCircleTransformation())
                .into(head_image);

感谢您的帮助

(应用程序在Android 7.0+上运行)

解决方法

使用SharedPreferences保存照片不是一个好主意。尝试将其缓存或使用本地数据库或文件系统存储和/或还原它们