调用摄像头拍照以及上传图片

 public void Photo() {
        head_tou = (ImageView) findViewById(R.id.img);
        head_tou.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 一个自定义的布局,作为显示的内容
                View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.photo_item,null);
                // 设置按钮的点击事件
                Button photo = (Button) contentView.findViewById(R.id.popwindow_photo);
                Button up = (Button) contentView.findViewById(R.id.popwindow_up);
                photo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, "button is pressed", Toast.LENGTH_SHORT).show();
                        // 拍照上传
                        Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(it, 1);
                    }
                });
                up.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Toast.makeText(MainActivity.this,"button is pressed",Toast.LENGTH_LONG).show();
                        // 本地上传实现:
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        //当Intent执行时,就会触发该行为
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(intent, 2);
                    }
                });
                final PopupWindow popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT, true);
                popupWindow.setTouchable(true);
                popupWindow.setTouchInterceptor(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        Log.i("mengdd", "onTouch : ");
                        return false;
                        // 这里如果返回true的话,touch事件将被拦截
                        // 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
                    }
                });

                // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
                // 我觉得这里是API的一个bug
//                popupWindow.setBackgroundDrawable(getResources().getDrawable(R.mipmap.ic_launcher));
                // 设置好参数之后再show
                popupWindow.showAsDropDown(head_tou);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            Log.d("TAG", "onActivityResult");

            switch (requestCode) {
                case 1:
                    Log.d("TAG", "camera");
                    Uri uri1 = data.getData();
                    Bitmap b = null;
                    if (uri1 != null) {
                        b = BitmapFactory.decodeFile(uri1.getPath());
                    } else {
                        Bundle extras = data.getExtras();
                        if (extras != null) {
                            b = (Bitmap) extras.get("data");
                        } else {
                           Toast.makeText(MainActivity.this,"获取图片失败",Toast.LENGTH_LONG).show();
                            return;
                        }
                    }
                    head_tou.setImageBitmap(b);
                    // 将Bitmap保存至SD卡
                    // 开启异步任务,上传至服务器
                    break;
                case 2:
                    Log.d("TAG", "pictrue");
                    Uri uri = data.getData();
                    Log.d("TAG", "uri: " + uri);
                    head_tou.setImageURI(uri);
                    break;
                default:
                    break;
            }
            ;
        }

你可能感兴趣的:(Android,开发)