调用系统相机/相册,实现更改头像

来源:互联网 发布:穿t恤不好看 知乎 编辑:程序博客网 时间:2024/05/22 10:47

为实现这个功能找了一些资料,推荐一篇

http://blog.sina.com.cn/s/blog_8a86f4dd01018jde.html

按照博主说的,一步一步做

选择框Dialog

/** * 选择相册dialog * Created by Cloud_android on 2017/4/11. */public class ChoiceAlbumDialog extends Dialog implements View.OnClickListener, AdapterView.OnItemClickListener {    Context context;    int layoutRes;//布局文件    private ListView mLvDialogChoice;    private TextView mCancel;    private List<String> datas = new ArrayList<>();    public ChoiceAlbumDialog(Context context, int theme, int resLayout) {        super(context, theme);        this.context = context;        this.layoutRes = resLayout;    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        getWindow().setGravity(Gravity.BOTTOM); //显示在底部        getWindow().getDecorView().setPadding(0, 0, 0, 0);        WindowManager.LayoutParams p = getWindow().getAttributes();        p.width = WindowManager.LayoutParams.MATCH_PARENT;        getWindow().setAttributes(p);        this.setContentView(layoutRes);        datas.add("选择相册");        datas.add("选择相机");        mCancel = (TextView) findViewById(R.id.tv_dialog_cancel);        mLvDialogChoice = (ListView) findViewById(R.id.lv_dialog_choice);        mCancel.setOnClickListener(this);        ChoiceAdapter adapter = new ChoiceAdapter(datas, getContext());        mLvDialogChoice.setAdapter(adapter);        mLvDialogChoice.setOnItemClickListener(this);    }    @Override    public void onClick(View view) {        this.dismiss();    }    @Override    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {        switch (i) {            //选择相册            case 0:                Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);                ((Activity) context).startActivityForResult(intent, 1001);                dismiss();                break;            //选择相机            case 1:                Intent intent_camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                ((Activity) context).startActivityForResult(intent_camera, 1002);                dismiss();                break;        }    }}

回调Activity 的onActivityResult()方法

    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        //相册        if (requestCode == 1001 && resultCode == Activity.RESULT_OK && data != null) {            System.out.println("系统相册");            if (requestCode == 1001 && resultCode == Activity.RESULT_OK && null != data) {                Uri selectedImage = data.getData();                String[] filePathColumns = {MediaStore.Images.Media.DATA};                Cursor c = this.getContentResolver().query(selectedImage, filePathColumns, null, null, null);                c.moveToFirst();                int columnIndex = c.getColumnIndex(filePathColumns[0]);                String picturePath = c.getString(columnIndex);                c.close();                //获取图片并显示                Glide.with(this).load(picturePath).into(mIvSettingIcon);            }        }        //相机        if (requestCode == 1002 && resultCode == Activity.RESULT_OK && null != data) {            System.out.println("系统相机");            String sdState = Environment.getExternalStorageState();            if (!sdState.equals(Environment.MEDIA_MOUNTED)) {                return;            }            new DateFormat();            String name = DateFormat.format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg";            Bundle bundle = data.getExtras();            //获取相机返回的数据,并转换为图片格式            Bitmap bitmap = (Bitmap) bundle.get("data");            FileOutputStream fos = null;            File file = new File("/sdcard/pintu/");            file.mkdirs();            String filename = file.getPath() + name;            try {                fos = new FileOutputStream(filename);                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);            } catch (FileNotFoundException e) {                e.printStackTrace();            } finally {                try {                    fos.flush();                    fos.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            //显示图片            Glide.with(this).load(filename).into(mIvSettingIcon);        }    }






0 1
原创粉丝点击