头像上传

public class InfoActivity extends BaseActivity implements ByUserIdInfoView {


    @BindView(R.id.my)
    TextView my;
    @BindView(R.id.myhead)
    TextView myhead;
    @BindView(R.id.img)
    ImageView img;`在这里插入代码片`
    @BindView(R.id.view1)
    View view1;
    @BindView(R.id.myname)
    TextView myname;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.view2)
    View view2;
    @BindView(R.id.mypwd)
    TextView mypwd;
    @BindView(R.id.pwd)
    TextView pwd;
    @BindView(R.id.view3)
    View view3;
    private String userId;
    private String sessionId;
    private ByUserIdInfoPresenter byUserIdInfoPresenter;

    @Override
    protected Object getPresenter() {
        return null;
    }

    @Override
    protected int layoutResID() {
        return R.layout.activity_info;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initData() {
        SharedPreferences cun = getSharedPreferences("cun", Context.MODE_PRIVATE);
        byUserIdInfoPresenter = new ByUserIdInfoPresenter(this);

        userId = cun.getString("userId", "");
        sessionId = cun.getString("sessionId", "");
        Log.d("sb_info", userId +"**"+ sessionId);
        byUserIdInfoPresenter.related(userId,sessionId);

        //头像上传
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final BottomSheetDialog dialog=new BottomSheetDialog(InfoActivity.this);
               // AlertDialog.Builder builder = new AlertDialog.Builder(InfoActivity.this);
                View inflate = View.inflate(InfoActivity.this, R.layout.bottm, null);
                dialog.setContentView(inflate);
                dialog.show();
                inflate.findViewById(R.id.camera).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                            //6.0权限
                            if(Build.VERSION.SDK_INT>23){
                                String[] strings = {Manifest.permission.CAMERA};
                                ActivityCompat.requestPermissions(InfoActivity.this,strings,123);
                            }
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.addCategory("android.intent.category.DEFAULT");
                        startActivityForResult(intent, 1);
                        dialog.dismiss();
                   }
                });

                inflate.findViewById(R.id.pic).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(Intent.ACTION_PICK);
                        intent.setType("image/*");
                        startActivityForResult(intent, 2);
                        dialog.dismiss();
                    }
                });
           }
        });

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1){
            Bitmap data1 = data.getParcelableExtra("data");
            img.setImageBitmap(data1);
            //将bitmap转成uri
            //将bitmap类型转换成uri
            Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), data1, null, null));
            addHeaderPhoto(uri);
        }

        if (requestCode==2){
            Uri data2 = data.getData();
            img.setImageURI(data2);
            addHeaderPhoto(data2);
        }
    }

    private void addHeaderPhoto(Uri uri) {
        //将uri类型转换成file
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String string = cursor.getString(columnIndex);
        File file = new File(string);

        //将file类型转换成part
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpg"), file);
        MultipartBody.Part part = MultipartBody.Part.createFormData("image", file.getName(), requestBody);


        ApiService service = RxjavaUtils.getInStence().getService(Api.nUrl, ApiService.class);
        Flowable<HeadPicBean> headpic = service.headpic(userId, sessionId, part);
        headpic.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<HeadPicBean>() {
                    @Override
                    public void onNext(HeadPicBean headPicBean) {
                        String headPath = headPicBean.getHeadPath();
                        String message = headPicBean.getMessage();
                        Toast.makeText(InfoActivity.this, message, Toast.LENGTH_SHORT).show();
                       //走个人信息
                        byUserIdInfoPresenter.related(userId,sessionId);
                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {
        
                    }
                });

    }

    //个人信息的数据
    @Override
    public void getUserIdInfo(ByUserIdGetInfo.ResultBean result) {
        Glide.with(InfoActivity.this).load(result.getHeadPic()).into(img);
        name.setText(result.getNickName());
        pwd.setText(result.getPassword());
    }
}

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值