安卓开发之头像的修改与拍照

个人信息页面

安卓开发之头像的修改与拍照_第1张图片

弹窗页面

 安卓开发之头像的修改与拍照_第2张图片

图库选择之后

安卓开发之头像的修改与拍照_第3张图片

拍照之后

安卓开发之头像的修改与拍照_第4张图片

1.个人信息xml




    

    

    

        

            

            
        

        
    

    


    

        

            

            
        

        
    

    

    

        

        
    

    

    

        

        
    

    


弹窗xml




    

    

    

    


2.java代码

package com.fb.hckjfb.activity;

import android.app.Dialog;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fb.hckjfb.R;
import com.fb.hckjfb.RoundTools;

import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * 个人信息
 */
public class MeActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_back, iv_head;
    private RelativeLayout rl_name, rl_head;
    private int SELECT_PICTURE = 0x00;
    private int SELECT_CAMER = 0x01;
    private Bitmap bitmap;
    private RoundTools roundTools;
    private Dialog bottomDialog;
    private TextView tv_tip, tv_name;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me);
        initView();
        //获取从上个页面传来的昵称并显示本页
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_me;
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initView() {
        iv_back = findViewById(R.id.iv_back);
        rl_name = findViewById(R.id.rl_name);
        rl_head = findViewById(R.id.rl_head);
        iv_head = findViewById(R.id.iv_head);
        tv_tip = findViewById(R.id.tv_tip);
        tv_name = findViewById(R.id.tv_name);

        iv_back.setOnClickListener(this);
        rl_name.setOnClickListener(this);
        rl_head.setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_name:
                String name = tv_name.getText().toString();
                intent = new Intent(this, SetNameActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_head:
                //选择对话框
                bottomDialog = new Dialog(this, R.style.BottomDialog);
                View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_content_normal, null);
                //获取Dialog的监听
                TextView tv_camera = (TextView) contentView.findViewById(R.id.tv_camera);
                TextView tv_chose = (TextView) contentView.findViewById(R.id.tv_chose);
                TextView tv_cancle = (TextView) contentView.findViewById(R.id.tv_cancle);
                tv_camera.setOnClickListener(this);
                tv_chose.setOnClickListener(this);
                tv_cancle.setOnClickListener(this);

                bottomDialog.setContentView(contentView);
                ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams();
                layoutParams.width = getResources().getDisplayMetrics().widthPixels;
                contentView.setLayoutParams(layoutParams);
                bottomDialog.getWindow().setGravity(Gravity.BOTTOM);//弹窗位置
                bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);//弹窗样式
                bottomDialog.show();//显示弹窗
                break;
            case R.id.tv_camera://自定义Doalog的点击事件
                //通过相机拍摄照片
                Intent intentCamer = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intentCamer, SELECT_CAMER);
                bottomDialog.dismiss();//取消弹窗
                //设置头像后提示字体不显示
                tv_tip.setText("");
                break;
            case R.id.tv_chose:
                //通过相册选择图片
                intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.addCategory(intent.CATEGORY_OPENABLE);
                intent.setType("image/*");
                startActivityForResult(intent.createChooser(intent, "选择图片"), SELECT_PICTURE);
                bottomDialog.dismiss();//取消弹窗
                //设置头像后提示字体不显示
                tv_tip.setText("");
                break;
            case R.id.tv_cancle:
                bottomDialog.dismiss(); //取消弹窗
                break;
            default:
                break;
        }
    }

    /**
     * 返回结果处理
     *
     * @param requestCode 请求代码
     * @param resultCode  结果代码
     * @param data        返回数据
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == SELECT_PICTURE) {
            Log.i(TAG, "相册");
            handle(resultCode, data);
            roundTools = new RoundTools();
            Bitmap roundAlbum = roundTools.toRoundBitmap(bitmap);
            iv_head.setImageBitmap(roundAlbum);
        } else if (requestCode == SELECT_CAMER) {
            Log.i(TAG, "相机");
            if (data.getData() == null) {
                bitmap = (Bitmap) data.getExtras().get("data");
                Log.i(TAG, "BitData    " + bitmap);
                roundTools = new RoundTools();
                Bitmap roundCamer = roundTools.toRoundBitmap(bitmap);
                iv_head.setImageBitmap(roundCamer);
            } else try {
                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
                if (bitmap != bitmap) {//主要是防止handle处理出错,就会将先前获取相册的照片show出来
                    iv_head.setImageBitmap(bitmap);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 数据处理 共同点提取
     *
     * @param resultCode
     * @param data
     */
    private void handle(int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {//结果代码是Ok的
            Uri uri = data.getData();
            if (uri != null && data.getData() != null) {
                Log.i(TAG, "uri 和 data.getData()不为空");
                ContentResolver contentResolver = this.getContentResolver();
                if (bitmap != null) {
                    bitmap.recycle();
                }
                try {
                    bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri));//出错
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            } else {
                Log.i(TAG, "uri为空或者data为空   " + "数据:" + data.getData() + "  uri: " + uri);
            }
        }
    }

}
3.style弹窗样式


    

    

anim进出效果







4.最后别忘记添加权限

 
    
    
    
    



你可能感兴趣的:(Android)