简单调用相机相册,并上传图片

废话不多说,直接上代码:

public class MineFragment extends Fragment implements IView {

@BindView(R.id.fresco_mine)
SimpleDraweeView frescoMine;
@BindView(R.id.choose_tv)
TextView chooseTv;
@BindView(R.id.name_tv)
TextView nameTv;
@BindView(R.id.name_tb)
TextView nameTb;
Unbinder unbinder;
private IPresenterImpl presenter;
private int uid;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_mine, null);
    EventBus.getDefault().register(this);
    unbinder = ButterKnife.bind(this, view);
    presenter = new IPresenterImpl(this);
    Map map = new HashMap<>();
    map.put("uid",uid+"");
    presenter.requseData(Consous.Base_Mine,map,null,MineBean.class);
    return view;
}
@Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
public void getInfo(LoginBean.DataBean bean){
    uid = bean.getUid();
    Toast.makeText(getContext(), uid+"", Toast.LENGTH_SHORT).show();
}

@OnClick({R.id.choose_tv, R.id.name_tb})
public void onViewClicked(View view) {
    switch (view.getId()) {
        case R.id.choose_tv:
            final View p = View.inflate(getContext(),R.layout.pop_item,null);
            final PopupWindow popupWindow = new PopupWindow(p,ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT,true);
            popupWindow.setOutsideTouchable(true);
            popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
            popupWindow.showAsDropDown(chooseTv);
            TextView xiangji = p.findViewById(R.id.xiangji);
            TextView pick = p.findViewById(R.id.pick);
            TextView cancle = p.findViewById(R.id.cancle);

            xiangji.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                //调用相机
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    intent.addCategory("android.intent.category.DEFAULT");
                    startActivityForResult(intent, 101);
                }
            });
            pick.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                //相册选取
                    Intent intent = new Intent(Intent.ACTION_PICK);
                    intent.setType("image/*");
                    startActivityForResult(intent,202);
                }
            });
            cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupWindow.dismiss();
                }
            });
            break;
        case R.id.name_tb:
            break;
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==101){
        Bitmap bitmap = data.getParcelableExtra("data");
        frescoMine.setImageBitmap(bitmap);
        //bitmap 转换 Uri 
        Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContext().getContentResolver(), bitmap, null,null));
        addHeaderPhoto(uri);
    }
    if(requestCode==202){
        Uri uri = data.getData();
        frescoMine.setImageURI(uri);

        addHeaderPhoto(uri);

    }
}
//获取图片位置 并上传
private void addHeaderPhoto(Uri uri) {
    String[] filePathColumn = {MediaStore.Images.Media.DATA};
    Cursor cursor = getActivity().getContentResolver().query(uri, filePathColumn, null, null, null);
    cursor.moveToFirst();
    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    //picturePath就是图片在储存卡所在的位置
    String string = cursor.getString(columnIndex);
    File file=new File(string);
    RequestBody requestBody =RequestBody.create(MediaType.parse("image/jpg"),file);
    MultipartBody.Part part=MultipartBody.Part.createFormData("file",file.getName(),requestBody);
    
    Map map = new HashMap<>();
    map.put("uid",uid+"");
    presenter.requseFileData(Consous.Base_Photo,map,part,null);
}

@Override
public void success(Object data) {
    if(data instanceof MineBean){
        MineBean bean = (MineBean) data;
        if(bean.getCode().equals("0")){
            String img = bean.getData().getIcon();
            if(img!=null){
            //https 和 http的字符替换
                img = img.replace("https","http");
                DraweeController controller = Fresco.newDraweeControllerBuilder()
                        .setUri(Uri.parse(img))
                        .build();
                 //图片赋值,Fresco实现
                frescoMine.setController(controller);
            }else{
                Toast.makeText(getContext(), "无头像", Toast.LENGTH_SHORT).show();
            }
        }else{
            Toast.makeText(getContext(), "失败", Toast.LENGTH_SHORT).show();
        }
    }else{
        Log.e("bean",data.toString());
    }
}

@Override
public void merror(String error) {
    Log.e("error",error);
}
@Override
public void onDestroyView() {
    super.onDestroyView();
    //释放资源
    unbinder.unbind();
    if(EventBus.getDefault().isRegistered(this)){
        EventBus.getDefault().unregister(this);
    }
}
}
//这是需要注意的Api
@Multipart
    @POST
    Observable post(@Url String murl, @QueryMap Map map, @Part MultipartBody.Part file);

简单的调用相机,有缘人,希望能帮到你。

你可能感兴趣的:(简单调用相机相册,并上传图片)