1. 程式人生 > >日期選擇器加Zxing

日期選擇器加Zxing

//manifest
<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="${applicationId}.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_provider_paths"/>
</provider>

//主頁面

public class One extends BaseFragment {
    @BindView(R.id.btn_sys)
    ImageView btn_sys;
    @BindView(R.id.textte)
    TextView textte;
    @BindView(R.id.timess)
    Button timess;
    private RxPermissions rxPermissions;
    @BindView(R.id.fresco_s)
    ImageView fresco_s;
    @Override
    protected int bindLayoutId() {
        return R.layout.one;
    }
    @OnClick(R.id.fresco_s)
    public void timess(){
       startActivity(new Intent(getActivity(), SetActivity.class));
    }
    @OnClick(R.id.timess)
    public void times(){
        new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                textte.setText("您的出生日期是:"+String.format("%d-%d-%d",year,monthOfYear+1,dayOfMonth));

            }
        },2000,1,2).show();

    }
    @OnClick(R.id.btn_sys)
    public void btn(){
        rxPermissions.requestEach(Manifest.permission.CAMERA)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted){
                            Intent intent = new Intent(getActivity(), CaptureActivity.class);
                            startActivityForResult(intent, 1000);
                        }else if (permission.shouldShowRequestPermissionRationale){
                            Toast.makeText(getActivity(),"您拒絕了請求將無法開啟應用",Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(getActivity(),"如果您想開啟此應用請去設定頁面開啟",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
    @Override
    protected void initData() {
        rxPermissions = new RxPermissions(this);
    }

}

//修改頭像的頁面

public class SetActivity extends BaseActivity {
    @BindView(R.id.imagesss)
    ImageView imagesss;
    private TakePictureManager takePictureManager;

    @OnClick(R.id.imagesss)
    public void imgs(){
        List<String> stringList = new ArrayList<>();
        stringList.add("拍照");
        stringList.add("從相簿選擇");
        final OptionBottomDialog optionBottomDialog = new OptionBottomDialog(SetActivity.this, stringList);
        optionBottomDialog.setItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:

                        takePictureManager = new TakePictureManager(SetActivity.this);
                        //開啟裁剪 比例 1:3 寬高 350 350  (預設不裁剪)
                        takePictureManager.setTailor(1, 3, 350, 350);
                        //拍照方式
                        takePictureManager.startTakeWayByCarema();
                        //回撥
                        takePictureManager.setTakePictureCallBackListener(new TakePictureManager.takePictureCallBackListener() {
                            //成功拿到圖片,isTailor 是否裁剪? ,outFile 拿到的檔案 ,filePath拿到的URl
                            @Override
                            public void successful(boolean isTailor, File outFile, Uri filePath) {

                                Picasso.with(SetActivity.this).load(outFile).error(R.mipmap.ic_launcher).into(imagesss);
                            }

                            //失敗回撥
                            @Override
                            public void failed(int errorCode, List<String> deniedPermissions) {
                                Log.e("==w",deniedPermissions.toString());
                            }
                        });
                        break;
                    case 1:
                        takePictureManager = new TakePictureManager(SetActivity.this);
                        takePictureManager.setTailor(1, 1, 350, 350);
                        takePictureManager.startTakeWayByAlbum();
                        takePictureManager.setTakePictureCallBackListener(new TakePictureManager.takePictureCallBackListener() {
                            @Override
                            public void successful(boolean isTailor, File outFile, Uri filePath) {
                                Picasso.with(SetActivity.this).load(outFile).error(R.mipmap.ic_launcher).into(imagesss);
                            }

                            @Override
                            public void failed(int errorCode, List<String> deniedPermissions) {

                            }

                        });
                        break;

                }
                optionBottomDialog.dismiss();
            }
        });
    }
    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_set;
    }

    //把本地的onActivityResult()方法回撥繫結到物件
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        takePictureManager.attachToActivityForResult(requestCode, resultCode, data);
    }

    //onRequestPermissionsResult()方法許可權回撥繫結到物件
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        takePictureManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

//上傳頭像

@POST("user/v1/verify/uploadHeadPic")
@Multipart
Observable<SetHeadPicEntity> uploadIcon(@Header("sessionId") String sessionId, @Header("userId") String userId, @Part MultipartBody.Part path);

//

//上傳頭像
@SuppressLint("CheckResult")
private void upLoadIcon(File myfile) {
    MultipartBody.Part filePart = MultipartBody.Part.createFormData("image", file.getName(), RequestBody.create(
            MediaType.parse("image/*"), myfile));//image/**/
    //得到loginsp裡面的userid,sessionid
    final SharedPreferences loginsp = getSharedPreferences("User", MODE_PRIVATE);
    String userId = loginsp.getString("userId", "");
    String sessionId = loginsp.getString("sessionId", "");
    String phone = loginsp.getString("phone", "");
    String pwd = loginsp.getString("pwd", "");
    final HashMap<String, String> map = new HashMap<>();
    map.put("phone",phone);
    map.put("pwd",pwd);
    RetrofitUtils.getInstance().createApi(Api.REGISTER, ApiAll.class).uploadIcon(sessionId, userId, filePart).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<SetHeadPicEntity>() {
        @Override
        public void accept(SetHeadPicEntity setHeadPicEntity) throws Exception {
        RetrofitUtils.getInstance().createApi(Api.LoginAll,ApiAll.class).Login(map).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<LoginEntity>() {
                    @Override
                    public void accept(LoginEntity loginEntity) throws Exception {
                        EventBus.getDefault().post("setHead");
                        String headPic = loginEntity.getResult().getUserInfo().getHeadPic();
                        edit.putString("headPic",headPic);
                        edit.commit();
                        EventBus.getDefault().post(new HeadEventBus(headPic));

                    }
                });


        }
    }, new Consumer<Throwable>() {
        @Override
        public void accept(Throwable throwable) throws Exception {
            Log.i("vvv", "失敗" + throwable.toString());
           Log.i( "aaaa" , throwable.toString());
        }
    });
}

//匯入

file_provider_paths

TakePictureManager