上传头像

我的信息Activity

package zjj.bwie.com.jx.me;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
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.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import zjj.bwie.com.jx.R;

public class MyInfoActivity extends AppCompatActivity {

    @BindView(R.id.img_me_myinfo_back)
    ImageView imgMeMyinfoBack;
    @BindView(R.id.img_me_myinfo_user)
    SimpleDraweeView imgMeMyinfoUser;
    @BindView(R.id.tv_me_myinfo_userid)
    TextView tvMeMyinfoUserid;
    @BindView(R.id.tv_me_myinfo_nickName)
    TextView tvMeMyinfoNickName;
    @BindView(R.id.btn_me_myinfo_logout)
    Button btnMeMyinfoLogout;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor edit;

    private Button btn_pop_takephotos;
    private View btn_popup_photos;

    private Uri imageUri;
    private ImageView picture;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_info);
        ButterKnife.bind(this);

        //创建sharedPreferences
        sharedPreferences = getSharedPreferences("me", MODE_PRIVATE);
        edit = sharedPreferences.edit();
        //给控件赋值
        String headPic = sharedPreferences.getString("headPic", "");
        imgMeMyinfoUser.setImageURI(headPic);
        int userId = sharedPreferences.getInt("userId", 0);
        tvMeMyinfoUserid.setText(String.valueOf(userId));
        String nickName = sharedPreferences.getString("nickName", "");
        tvMeMyinfoNickName.setText(nickName);

    }

    @OnClick({R.id.img_me_myinfo_back, R.id.img_me_myinfo_user, R.id.btn_me_myinfo_logout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_me_myinfo_back:
                finish();
                break;
            case R.id.img_me_myinfo_user:
                //跟换头像
                getpopup();

                break;
            case R.id.btn_me_myinfo_logout:
                edit.clear();
                edit.commit();

                //发送广播提示MeFragment刷新
                Intent intent = new Intent("MeFragment");
                intent.putExtra("MeFragment","MeFragment");
                LocalBroadcastManager.getInstance(MyInfoActivity.this).sendBroadcast(intent);
                sendBroadcast(intent);

                finish();
                break;
        }
    }

    private void getpopup() {
        View popview = LayoutInflater.from(MyInfoActivity.this).inflate(R.layout.activity_popup, null, false);
        //寻找控件
        btn_pop_takephotos = popview.findViewById(R.id.btn_popup_takephoto);
        btn_popup_photos = popview.findViewById(R.id.btn_popup_photos);
        final PopupWindow popupWindow = new PopupWindow(popview, AbsListView.LayoutParams.MATCH_PARENT, 300, true);
        // 设置PopupWindow的背景
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        // 设置PopupWindow是否能响应外部点击事件
        popupWindow.setOutsideTouchable(true);
        // 设置PopupWindow是否能响应点击事件
        popupWindow.setTouchable(true);
        // 设置popupWindow的显示位置,此处是在手机屏幕底部且水平居中的位置
        popupWindow.showAtLocation(MyInfoActivity.this.imgMeMyinfoUser,Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

        //拍照
        btn_pop_takephotos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File outputImage = new File(Environment.getExternalStorageDirectory(),
                        "tempImage" + ".jpg");
                try {
                    if (outputImage.exists()) {
                        outputImage.delete();
                    }
                    outputImage.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                imageUri = Uri.fromFile(outputImage);
                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(intent, 1);
            }
        });

        //相册选择
        btn_popup_photos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent, 1);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            //裁剪
            case 0:
                if (resultCode == RESULT_OK) {
                    Intent intent = new Intent("com.android.camera.action.CROP");
                    //设置裁剪之后的图片路径文件
                    File cutfile = new File(Environment.getExternalStorageDirectory().getPath(),
                            "cutcamera.png"); //随便命名一个
                    if (cutfile.exists()){ //如果已经存在,则先删除,这里应该是上传到服务器,然后再删除本地的,没服务器,只能这样了
                        cutfile.delete();
                    }
                    try {
                        cutfile.createNewFile();
                        //初始化 uri
                        Uri imageuri =imageUri; //返回来的 uri
                        Uri outputUri = null; //真实的 uri
                        outputUri = Uri.fromFile(cutfile);
                        intent.setDataAndType(imageUri, "image/*");
                        intent.putExtra("crop", true);
                        // aspectX,aspectY 是宽高的比例,这里设置正方形
                        intent.putExtra("aspectX",1);
                        intent.putExtra("aspectY",1);

                        intent.putExtra("scale", true);
                        intent.putExtra("return-data",false);
                        intent.setDataAndType(imageUri, "image/*");
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
                        intent.putExtra("noFaceDetection", true);
                        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
                        startActivityForResult(intent, 1); // 启动裁剪程序
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                break;
                //设置更改后的图片
            case 1:
                if (resultCode == RESULT_OK) {

                    if(data != null) {
                        Uri uri = data.getData();
                        imageUri = uri;
                    }
                    imgMeMyinfoUser.setImageURI(imageUri);
                    //imgMeMyinfoUser.setImageBitmap(bitmap);
                }
                break;
        }

    }
}

popup布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:background="#909090"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:id="@+id/btn_popup_takephoto"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="拍照"
        />
    <Button
        android:id="@+id/btn_popup_photos"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="相册"
        />
</LinearLayout>

 清单注册

<activity android:name=".me.MyInfoActivity">
            <intent-filter>
                <action android:name="android.media.action.IMAGE_CAPTURE"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值