1. 程式人生 > >上傳頭像,相機相簿檔案轉換為File檔案

上傳頭像,相機相簿檔案轉換為File檔案

 

1,彈出dialog  相機按鈕,相簿按鈕,

//彈出dialog
    private void getDialog() {
        dialog = new Dialog(this);
        //填充對話方塊的佈局
        View inflate = LayoutInflater.from(this).inflate(R.layout.popup_window_button, null);
        //初始化控制元件
        inflate.findViewById(R.id.takePhoto).setOnClickListener(this);
        inflate.findViewById(R.id.choosePhoto).setOnClickListener(this);
        inflate.findViewById(R.id.btn_cancel).setOnClickListener(this);
        //將佈局設定給Dialog
        dialog.setContentView(inflate);
        //獲取當前Activity所在的窗體
        Window dialogWindow = dialog.getWindow();
        if (dialogWindow == null) {
            return;
        }
        //設定Dialog從窗體底部彈出
        dialogWindow.setGravity(Gravity.BOTTOM);
        //獲得窗體的屬性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.y = 10;//設定Dialog距離底部的距離
        //將屬性設定給窗體
        dialogWindow.setAttributes(lp);
        dialog.show();//顯示對話方塊
    }

 

2,跳轉相機,相簿的點選事件

 @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.takePhoto:
                dialog.dismiss();
                File f = new File(imgRoot, new Date().getTime() + ".jpg");
                uri = Uri.fromFile(f);
// ACTION_IMAGE_CAPTURE(動作影象捕獲)
                intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 拍照輸出的路徑 (EXTRA_OUTPUT額外輸出)
                intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                startActivityForResult(intent, FLAG_CAMERA_REQUEST);
                break;
            case R.id.choosePhoto:
                dialog.dismiss();
                //case R.id.btn_choose_album:
                intent = new Intent(Intent.ACTION_PICK);
// 設定型別 image/jpeg image/png image/gif,*是萬用字元
                intent.setType("image/*");
                startActivityForResult(intent, FFLAG_ALBUM_REQUEST);
                break;

 

 

在ActivityResult方法裡 獲取到uri,轉換為file檔案

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

        if (data != null) {
            Uri uri = data.getData();
            String[] filePathColumns = {MediaStore.Images.Media.DATA};
            Cursor c = getContentResolver().query(uri, filePathColumns, null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePathColumns[0]);
            String imagePath = c.getString(columnIndex);
            Uri uri2 = new Uri.Builder()
                    .scheme(UriUtil.LOCAL_FILE_SCHEME)
                    .path(imagePath)
                    .build();
            instanll_img_logo.setImageURI(uri2);

            //選擇是否存入sp ,
            SharedPreferences sp = getContext().getSharedPreferences("user", 0);
            SharedPreferences.Editor edit = sp.edit();
            edit.putString("icon", String.valueOf(uri2));
            edit.commit();

            c.close();
            file = new File(imagePath);

            loadImg(file);
            //呼叫P層方法,傳值
            presenter.upLoad(uid, file);
            

 

M層

 public Observable<UpLoadBean> upLoad(int uid, File file) {

        ILoginBean iLoginBean = RetrofitManager.getInstence().create(ILoginBean.class);

        //RequestBody封裝了檔案和檔案的型別
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
        // MultipartBody.Part封裝了接受的key和檔名字和RequestBody
        MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
        return iLoginBean.upLoad(uid, part);
    }

P層

 @SuppressLint("CheckResult")
    public void upLoad(int uid, File file) {
        model.upLoad(uid,file)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<UpLoadBean>() {
                    @Override
                    public void accept(UpLoadBean upLoadBean) throws Exception {
                        if (iView != null){
                            if (upLoadBean != null& "0".equals(upLoadBean.getCode())){
                                iView.onSuccess(upLoadBean);
                            }

                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        if (iView != null){
                            iView.onFailed(throwable);
                        }
                    }
                });

p層呼叫view層返回結果的方法。  轉換結束。