上传头像

1.MainActivity
public class MainActivity extends BaseActivityPresenter<MainActivityPresenter> {


    @Override
    public Class<MainActivityPresenter> getGate() {
        return MainActivityPresenter.class;
    }

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


2.MainActivityPresenter

    public void init(int requestCode, int resultCode, Intent data) {
        Fragment fragment = list.get(mViewPager.getCurrentItem());
        if (fragment instanceof FragmentOne){
            ((FragmentOne)fragment).onActivity(requestCode,resultCode,data);
        }
    }
}
3.framentOne

    public void onActivity(int requestCode, int resultCode, Intent data) {
        getGate.startForResult(requestCode,resultCode,data);
    }
4.FramentOnePresenter
public class FragmentOnePresenter extends AppCreateIml implements View.OnClickListener {
    private ImageView photo;
    private View contextView;
    private Button btn_xc;
    private Button btn_xj;
    private PopupWindow window;
    private View parent;
    private String path = Environment.getExternalStorageDirectory()
            + "/head.jpg";

    @Override
    public int getLayoutId() {
        return R.layout.fragment_fragment_one;
    }

    @Override
    public void initData() {
        //初始化控件
        photo=(ImageView)get(R.id.photo);
        parent = View.inflate(((MainActivity)context), R.layout.activity_main, null);
        //popwindow
        contextView = View.inflate(context, R.layout.layout_pop, null);
        window = new PopupWindow(contextView, LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        window.setTouchable(true);
        window.setFocusable(true);
        window.setOutsideTouchable(true);
        window.setBackgroundDrawable(new BitmapDrawable());
        btn_xc=(Button)contextView.findViewById(R.id.btn_xc);
        btn_xj=(Button)contextView.findViewById(R.id.btn_xj);
        btn_xj.setOnClickListener(this);
        btn_xc.setOnClickListener(this);
        photo.setOnClickListener(this);
    }
    private Context context;
    @Override
    public void getContext(Context context) {
        this.context=context;
    }

    

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            //点击图片
            case R.id.photo:
                window.showAtLocation(parent, Gravity.BOTTOM,0,0);
                break;
            //点击相册
            case R.id.btn_xc:
                Intent xcIntent = new Intent(Intent.ACTION_PICK);
                xcIntent.setType("image/*");
                ((MainActivity)context).startActivityForResult(xcIntent, 89);
                break;
            //点击相机
            case R.id.btn_xj:
                Intent xjintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                xjintent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
                ((MainActivity)context).startActivityForResult(xjintent, 99);
                break;
        }
    }
    //调用相机和相册
    public void startForResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 89 && resultCode ==  RESULT_OK) {
            crop(data.getData());
        } else if (requestCode == 100 && resultCode == RESULT_OK) {
            Bitmap bitmap = data.getParcelableExtra("data");
            if (bitmap == null) {
                Toast.makeText(context,"哈哈哈",Toast.LENGTH_LONG).show();
            }
            photo.setImageBitmap(bitmap);
        } else if (requestCode == 99 && resultCode == RESULT_OK) {
            Uri uri = Uri.fromFile(new File(path));
            crop(uri);
        }
    }

    //对相册的裁剪
    private void crop(Uri data) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(data, "image/*");
        intent.putExtra("crop", true);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("output", 251);
        intent.putExtra("output", 251);
        intent.putExtra("return-data", true);
        ((MainActivity)context).startActivityForResult(intent, 100);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值