欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

登陆相机

程序员文章站 2022-04-18 18:35:33
...

package com.bwie.jd.view.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.bwie.jd.R;
import com.bwie.jd.model.bean.FenRightBean;
import com.bwie.jd.model.bean.HomeBean;
import com.bwie.jd.presenter.MainPresenter;
import com.bwie.jd.view.IView.IMainView;
import com.bwie.jd.view.activity.LoginActivity;
import com.bwie.jd.view.adapter.TuiAdapter;
import com.facebook.drawee.view.SimpleDraweeView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static android.app.Activity.RESULT_OK;

public class MyFragment extends Fragment implements IMainView {
@BindView(R.id.img)
SimpleDraweeView img;
@BindView(R.id.deng)
TextView deng;
Unbinder unbinder;
@BindView(R.id.rv_product_my)
RecyclerView rec;
private Uri uri;
private PopupWindow window;
private MainPresenter mainPresenter;
private Bitmap head;// 头像Bitmap
private static String path = “/sdcard/myHead/”;// sd路径
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my, container, false);
unbinder = ButterKnife.bind(this, view);
path = Environment.getExternalStorageDirectory() + “/head.jpg”;
mainPresenter = new MainPresenter();
mainPresenter.attachView(this);
mainPresenter.loadDataFromNet();
return view;
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();
}


@OnClick({R.id.img, R.id.deng})
public void onViewClicked(View view) {
    switch (view.getId()) {
        case R.id.img:
            //得到更换头像的popwindow
            showPopwindow();
            break;
        case R.id.deng:
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);

            break;
    }
}

private void showPopwindow() {
    // 利用layoutInflater获得View
    LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.linearlayout, null);
    // 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()
    window = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.WRAP_CONTENT);
    // 设置popWindow弹出窗体可点击,这句话必须添加,并且是true
    window.setFocusable(true);

    // 实例化一个ColorDrawable颜色为半透明
    ColorDrawable dw = new ColorDrawable(0xb0000000);
    window.setBackgroundDrawable(dw);

    // 设置popWindow的显示和消失动画
    //window.setAnimationStyle(R.style.mypopwindow_anim_style);
    // 在底部显示
    window.showAtLocation(getActivity().findViewById(R.id.img), Gravity.BOTTOM, 0, 0);
    // 这里检验popWindow里的button是否可以点击
    Button first = view.findViewById(R.id.first);//相机
    Button third = view.findViewById(R.id.third);//取消
    Button second = view.findViewById(R.id.second);//相册
    //相机
    first.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "head.jpg")));
            startActivityForResult(intent, 2);
            window.dismiss();
        }
    });

    //相册
    second.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK, null);
            intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
            startActivityForResult(intent, 1);
            window.dismiss();
        }
    });

    //取消
    third.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(), "取消", Toast.LENGTH_SHORT).show();
            window.dismiss();
        }
    });
    // popWindow消失监听方法
    window.setOnDismissListener(new PopupWindow.OnDismissListener() {

        @Override
        public void onDismiss() {
            System.out.println("popWindow消失");
        }
    });

}

@Override
public void onSuccess(HomeBean.DataBean data) {
    List<HomeBean.DataBean.TuijianBean.ListBeanX> list1 = data.getTuijian().getList();
    GridLayoutManager manager1 = new GridLayoutManager(getActivity(), 2, GridLayoutManager.VERTICAL, false);
    rec.setLayoutManager(manager1);
    rec.setAdapter(new TuiAdapter(getActivity(), list1));
}

@Override
public void onErr(int errCode, String errMsg) {

}

@Override
public void rightSuccess(List<FenRightBean.DataBean> msg) {

}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    switch (requestCode) {
        case 1:
            if (resultCode == RESULT_OK) {
                cropPhoto(data.getData());// 裁剪图片
            }

            break;
        case 2:
            if (resultCode == RESULT_OK) {
                File temp = new File(Environment.getExternalStorageDirectory() + "/head.jpg");
                cropPhoto(Uri.fromFile(temp));// 裁剪图片
            }

            break;
        case 3:
            if (data != null) {
                Bundle extras = data.getExtras();
                head = extras.getParcelable("data");
                if (head != null) {
                    /**
                     * 上传服务器代码
                     */
                    setPicToView(head);// 保存在SD卡中
                    img.setImageBitmap(head);// 用ImageView显示出来
                }
            }
            break;
        default:
            break;

    }
    super.onActivityResult(requestCode, resultCode, data);

//
if (requestCode == 999 && resultCode == 1) {
String mobile = data.getExtras().getString(“mobile”);
deng.setText(mobile);
} else {
if (requestCode == 999 && resultCode == 888) {
String name = data.getExtras().getString(“name”);
String iconurl = data.getExtras().getString(“iconurl”);
deng.setText(name);
Uri parse = Uri.parse(iconurl);
img.setImageURI(parse);
}
}

}

private void cropPhoto(Uri data) {
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(data, "image/*");
    intent.putExtra("crop", "true");
    // aspectX aspectY 是宽高的比例
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    // outputX outputY 是裁剪图片宽高
    intent.putExtra("outputX", 150);
    intent.putExtra("outputY", 150);
    intent.putExtra("return-data", true);
    startActivityForResult(intent, 3);
}

private void setPicToView(Bitmap mBitmap) {
    String sdStatus = Environment.getExternalStorageState();
    if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
        return;
    }
    FileOutputStream b = null;
    File file = new File(path);
    file.mkdirs();// 创建文件夹
    String fileName = path + "head.jpg";// 图片名字
    try {
        b = new FileOutputStream(fileName);
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        try {
            // 关闭流
            b.flush();
            b.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}

}