package com.ysvideo.zhibo.app.ui.mine;
|
|
import android.Manifest;
|
import android.annotation.SuppressLint;
|
import android.app.Activity;
|
import android.app.AlertDialog;
|
import android.content.ActivityNotFoundException;
|
import android.content.Context;
|
import android.content.DialogInterface;
|
import android.content.Intent;
|
import android.content.SharedPreferences;
|
import android.content.pm.PackageManager;
|
import android.graphics.Bitmap;
|
import android.graphics.BitmapFactory;
|
import android.graphics.Color;
|
import android.net.Uri;
|
import android.os.Bundle;
|
import android.os.Environment;
|
import android.provider.MediaStore;
|
import android.util.Base64;
|
import android.util.Log;
|
import android.view.Gravity;
|
import android.view.View;
|
import android.widget.DatePicker;
|
import android.widget.EditText;
|
import android.widget.ImageView;
|
import android.widget.LinearLayout;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.androidquery.AQuery;
|
import com.bumptech.glide.Glide;
|
import com.ysvideo.zhibo.app.R;
|
import com.ysvideo.zhibo.app.ui.common.BrowserActivity;
|
import com.ysvideo.zhibo.app.util.UserUtil;
|
import com.ysvideo.zhibo.app.util.api.BasicTextHttpResponseHandler;
|
import com.ysvideo.zhibo.app.util.api.HttpApiUtil;
|
import com.ysvideo.zhibo.app.util.ui.TopBarUtil;
|
import com.ysvideo.zhibo.lib.common.activity.BaseActivity;
|
import com.ysvideo.zhibo.lib.common.util.SingleToast;
|
import com.ysvideo.zhibo.lib.common.util.common.DimenUtils;
|
import com.ysvideo.zhibo.lib.common.util.common.PhotoCrop;
|
import com.ysvideo.zhibo.lib.common.util.common.StringUtils;
|
import com.ysvideo.zhibo.lib.common.util.glide.GlideCircleTransform;
|
import com.ysvideo.zhibo.lib.common.util.ui.SystemClipImageUtil;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayOutputStream;
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.FileNotFoundException;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.util.Calendar;
|
|
import androidx.core.app.ActivityCompat;
|
import androidx.core.content.ContextCompat;
|
|
/**
|
* Created by weikou2015 on 2016/10/27.
|
*/
|
|
public class PersonInfoActivity extends BaseActivity implements View.OnClickListener {
|
|
private static final String TAG = "PersonInfoActivity";
|
|
private LinearLayout ll_portrait;
|
private LinearLayout ll_nickName;
|
private LinearLayout ll_birthday;
|
private LinearLayout ll_sex;
|
private LinearLayout ll_autograph;
|
|
private ImageView iv_portrait;
|
private TextView tv_nickName;
|
private TextView tv_birthday;
|
private TextView tv_sex;
|
private TextView tv_autograph;
|
private TextView tv_email;
|
|
private TextView tv_top_bar_right;
|
|
private AQuery mAQuery;
|
|
|
@Override
|
protected void onCreate(Bundle arg0) {
|
super.onCreate(arg0);
|
setContentView(R.layout.activity_person_info);
|
TopBarUtil.init(this, "个人信息", this);
|
|
mAQuery = new AQuery(this);
|
|
|
tv_top_bar_right = findViewById(R.id.tv_top_bar_right);
|
tv_top_bar_right.setOnClickListener(this);
|
tv_top_bar_right.setText("保存");
|
ll_portrait = findViewById(R.id.ll_portrait_select);
|
ll_nickName = findViewById(R.id.ll_nickName);
|
ll_birthday = findViewById(R.id.ll_birthday);
|
ll_sex = findViewById(R.id.ll_sex);
|
ll_autograph = findViewById(R.id.ll_autograph);
|
iv_portrait = findViewById(R.id.iv_portrai_select);
|
tv_nickName = findViewById(R.id.tv_nickName);
|
tv_birthday = findViewById(R.id.tv_birthday);
|
tv_sex = findViewById(R.id.tv_sex);
|
tv_autograph = findViewById(R.id.tv_autograph);
|
tv_email = findViewById(R.id.tv_email);
|
|
ll_portrait.setOnClickListener(this);
|
ll_nickName.setOnClickListener(this);
|
ll_birthday.setOnClickListener(this);
|
ll_sex.setOnClickListener(this);
|
ll_autograph.setOnClickListener(this);
|
findViewById(R.id.ll_advice).setOnClickListener(this);
|
getPersonInfo();
|
}
|
|
private String sign = "";
|
|
private void getPersonInfo() {
|
HttpApiUtil.getPersonInfo(this, UserUtil.getLoginUid(getApplicationContext()), new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (jsonObject.optBoolean("IsPost")) {
|
JSONObject data = jsonObject.optJSONObject("Data");
|
Glide.with(PersonInfoActivity.this).load(data.optString("Portrait")).transform(new GlideCircleTransform(getApplicationContext())).placeholder(R.drawable.ic_portrait_default).error(R.drawable.ic_portrait_default).into(iv_portrait);
|
tv_nickName.setText(data.optString("Nickname"));
|
tv_birthday.setText(data.optString("Birthday"));
|
if (!StringUtils.isEmpty(data.optString("Sex"))) {
|
tv_sex.setText(Integer.parseInt(data.optString("Sex")) == 0 ? "女" : "男");
|
}
|
sign = data.optString("Sign");
|
String email = data.optString("Email");
|
if (StringUtils.isEmpty(email)) {
|
tv_email.setText("暂无");
|
} else {
|
tv_email.setText(email);
|
}
|
mAQuery.id(R.id.tv_id).text(data.optString("Id"));
|
tv_autograph.setText(sign);
|
}
|
}
|
});
|
}
|
|
String imgBase64 = "";
|
|
@Override
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
// TODO Auto-generated method stub
|
super.onActivityResult(requestCode, resultCode, data);
|
if (resultCode == Activity.RESULT_OK) {
|
//从相册中选择而来
|
if (requestCode == 1001 && data != null) {
|
Uri selectedImage = data.getData();
|
startPhotoZoom(selectedImage);//系统剪裁
|
} else if (requestCode == 1002 && data != null) {
|
if (uriClipUri != null) {
|
String path = "";
|
if (!StringUtils.isEmpty(uriClipUri.getAuthority())) {
|
try {
|
imgBase64 = bitmapToBase64(getContentResolver().openInputStream(uriClipUri));
|
Glide.with(PersonInfoActivity.this).load(uriClipUri).transform(new GlideCircleTransform(PersonInfoActivity.this)).into(iv_portrait);
|
} catch (FileNotFoundException e) {
|
e.printStackTrace();
|
}
|
} else {
|
path = uriClipUri.getPath();
|
//图库
|
PhotoCrop crop = new PhotoCrop();//初始化 图片压缩
|
File f = new File(path);//获取图片
|
if (crop.compressPhto(f, false)) {//压缩并保存成功
|
imgBase64 = bitmapToBase64(f);
|
}
|
|
iv_portrait.postDelayed(new Runnable() {
|
@Override
|
public void run() {
|
Glide.with(PersonInfoActivity.this).load(uriClipUri).transform(new GlideCircleTransform(PersonInfoActivity.this)).into(iv_portrait);
|
}
|
}, 200);
|
}
|
}
|
}
|
}
|
}
|
|
|
private Uri uriClipUri;//剪裁图片路径
|
|
/**
|
* 获取相册图片
|
*
|
* @param uri
|
*/
|
public void startPhotoZoom(Uri uri) {
|
File storageDir = new File(android.os.Environment.getExternalStorageDirectory().getPath() + "/HanJuImg/");
|
if (!storageDir.exists()) {//没有文件夹则创建
|
storageDir.mkdir();
|
}
|
uriClipUri = SystemClipImageUtil.startPhotoZoom(this, uri, "clip.jpg", 250, 250, 1002);
|
return;
|
}
|
|
private String bitmapToBase64(InputStream in) {
|
try {
|
byte[] bytes = new byte[in.available()];
|
int length = in.read(bytes);
|
return Base64.encodeToString(bytes, 0, length, Base64.DEFAULT);
|
} catch (FileNotFoundException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
try {
|
if (in != null) {
|
in.close();
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return null;
|
}
|
|
private String bitmapToBase64(File file) {
|
try {
|
return bitmapToBase64(new FileInputStream(file));
|
} catch (FileNotFoundException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
private String saveBitmapTofile(Bitmap bm) {
|
String name = "img.png";
|
File fDir = new File(Environment.getExternalStorageDirectory() + "");
|
if (!fDir.exists()) {
|
fDir.mkdir();
|
}
|
File picFile = new File(fDir, name);
|
try {
|
FileOutputStream out = new FileOutputStream(picFile);
|
bm.compress(Bitmap.CompressFormat.PNG, 100, out);
|
out.flush();
|
out.close();
|
} catch (FileNotFoundException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return picFile.getPath();
|
}
|
|
@SuppressLint("SdCardPath")
|
|
private int imgWidth = 400;
|
|
private int imgHeight = 400;
|
|
private int maxSize = 300;
|
|
/*
|
* 压缩图片
|
*/
|
private Bitmap compressImage(Bitmap image) {
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
|
int options = 100;
|
while (baos.toByteArray().length / 1024 > maxSize) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
|
baos.reset();// 重置baos即清空baos
|
image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中
|
options -= 10;// 每次都减少10
|
}
|
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
|
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片
|
return bitmap;
|
}
|
|
// 判断权限集合 是否授权 false授权 true未授权
|
public boolean lacksPermissions(String... permissions) {
|
for (String permission : permissions) {
|
if (lacksPermission(permission)) {
|
return true;
|
}
|
}
|
return false;
|
}
|
|
// 判断是否缺少权限
|
private boolean lacksPermission(String permission) {
|
//权限未授权
|
return ContextCompat.checkSelfPermission(getApplicationContext(), permission) == PackageManager.PERMISSION_DENIED;
|
}
|
|
@Override
|
public void onClick(View v) {
|
switch (v.getId()) {
|
case R.id.tv_top_bar_left:
|
finish();
|
break;
|
case R.id.ll_portrait_select:
|
if (lacksPermissions(Manifest.permission.READ_EXTERNAL_STORAGE,
|
Manifest.permission.WRITE_EXTERNAL_STORAGE)) { //存储权限未开启
|
ActivityCompat.requestPermissions(PersonInfoActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 10023);
|
return;
|
}
|
popwindow_img();//选择相册
|
break;
|
case R.id.ll_nickName:
|
getNickName();
|
break;
|
case R.id.ll_birthday:
|
getDate();
|
break;
|
case R.id.ll_sex:
|
getSex();
|
break;
|
case R.id.ll_autograph://个性签名
|
getAutograph();
|
break;
|
case R.id.tv_top_bar_right://完成个人信息的修改
|
upLoadRegisterData();
|
break;
|
case R.id.ll_advice:
|
startActivity(new Intent(getApplicationContext(), BrowserActivity.class).putExtra("title", "意见反馈").putExtra("url", "https://support.qq.com/embed/phone/343178/new-post/"));
|
|
break;
|
}
|
}
|
|
|
/**
|
* 从相册选择 图片
|
*/
|
private void popwindow_img() {
|
// 打开系统图库选择图片
|
Intent picture = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
|
//解决手机找不到activity 方案一 检测裁是否有对应的app(选择了方案二 方案一占时屏蔽)
|
// PackageManager packageManager = getPackageManager();
|
// List<ResolveInfo> applist = packageManager.queryIntentActivities(picture, 0);
|
// if (applist == null || applist.isEmpty()) {
|
// Toast.makeText(MyInfoActivity.this, "no activity", Toast.LENGTH_SHORT).show();
|
// return;
|
// }
|
//解决手机找不到activity 方案二 抛异常
|
try {
|
startActivityForResult(picture, 1001);
|
} catch (ActivityNotFoundException exception) {
|
Toast.makeText(this, "no activity", Toast.LENGTH_SHORT).show();
|
}
|
|
|
}
|
|
/**
|
* 上传个人信息
|
*/
|
private void upLoadRegisterData() {
|
|
HttpApiUtil.upLoadPersonInfo(this, UserUtil.getLoginUid(getApplicationContext()), mSex + "", tv_birthday.getText().toString(),
|
tv_autograph.getText().toString(), imgBase64, tv_nickName.getText().toString(), new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (jsonObject.optBoolean("IsPost")) {
|
SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
|
SharedPreferences.Editor editor = sharedPreferences.edit();
|
editor.putString("name", tv_nickName.getText().toString());
|
editor.commit();
|
|
SingleToast.showToast(PersonInfoActivity.this, "个人信息更新成功");
|
finish();
|
}
|
}
|
});
|
}
|
|
private int mSex = 0;
|
|
private void getSex() {
|
if (isFinishing())
|
return;
|
final String[] sex = new String[]{"女", "男"};
|
new AlertDialog.Builder(this).setTitle("性别选择").setSingleChoiceItems(
|
sex, 0, new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialog, int which) {
|
mSex = which;
|
}
|
})
|
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialog, int which) {
|
Log.i("mResult", "which的值为:" + which);
|
tv_sex.setText(sex[mSex]);
|
dialog.dismiss();
|
}
|
})
|
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialog, int which) {
|
dialog.dismiss();
|
}
|
}).show();
|
}
|
|
private void getAutograph() {
|
final EditText input = new EditText(this);
|
input.setLines(5);
|
input.setBackground(null);
|
input.setTextColor(Color.parseColor("#FF555555"));
|
input.setGravity(Gravity.START);
|
input.setText(sign);
|
input.setPadding(DimenUtils.dip2px(getApplicationContext(), 20), DimenUtils.dip2px(getApplicationContext(), 5), DimenUtils.dip2px(getApplicationContext(), 20), 0);
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
builder.setTitle("个性签名修改").setView(input)
|
.setNegativeButton("取消", null);
|
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
public void onClick(DialogInterface dialog, int which) {
|
sign = input.getText().toString();
|
tv_autograph.setText(sign);
|
|
}
|
});
|
|
builder.create().show();
|
|
|
}
|
|
private void getNickName() {
|
final EditText et_msg = new EditText(this);
|
if (isFinishing())
|
return;
|
|
final EditText input = new EditText(this);
|
input.setLines(1);
|
input.setBackground(null);
|
input.setTextColor(Color.parseColor("#FF555555"));
|
input.setGravity(Gravity.START);
|
input.setText(tv_nickName.getText());
|
input.setPadding(DimenUtils.dip2px(getApplicationContext(), 20), DimenUtils.dip2px(getApplicationContext(), 5), DimenUtils.dip2px(getApplicationContext(), 20), 0);
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
builder.setTitle("昵称修改").setView(input)
|
.setNegativeButton("取消", null);
|
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
public void onClick(DialogInterface dialog, int which) {
|
String text = input.getText().toString();
|
tv_nickName.setText(text);
|
}
|
});
|
|
builder.create().show();
|
}
|
|
/**
|
* 获取生日
|
*/
|
String date = "";
|
|
private void getDate() {
|
|
final DatePicker datePicker = new DatePicker(this);
|
Calendar calendar = Calendar.getInstance();
|
int year = calendar.get(Calendar.YEAR);
|
int monthOfYear = calendar.get(Calendar.MONTH);
|
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
|
date = year + "/" + monthOfYear + "/" + dayOfMonth;
|
datePicker.init(year, monthOfYear, dayOfMonth, new DatePicker.OnDateChangedListener() {
|
@Override
|
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
|
date = year + "/" + (monthOfYear + 1) + "/" + dayOfMonth;
|
}
|
});
|
datePicker.setPadding(DimenUtils.dip2px(getApplicationContext(), 20), DimenUtils.dip2px(getApplicationContext(), 5), DimenUtils.dip2px(getApplicationContext(), 20), 0);
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
builder.setTitle("生日修改").setView(datePicker)
|
.setNegativeButton("取消", null);
|
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
public void onClick(DialogInterface dialog, int which) {
|
tv_birthday.setText(date);
|
}
|
});
|
|
builder.create().show();
|
|
|
// View view = LayoutInflater.from(this).inflate(R.layout.item_datepicker, null);
|
// if (isFinishing())
|
// return;
|
// final AlertDialog alertDialog = new AlertDialog.Builder(PersonInfoActivity.this).setView(view).create();
|
// alertDialog.show();
|
// DatePicker datePicker = view.findViewById(R.id.datePicker);
|
// TextView tv_cancel = view.findViewById(R.id.tv_cancel);
|
// final TextView tv_confirm = view.findViewById(R.id.tv_confirm);
|
//
|
// Calendar calendar = Calendar.getInstance();
|
// int year = calendar.get(Calendar.YEAR);
|
// int monthOfYear = calendar.get(Calendar.MONTH);
|
// int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
|
// date = year + "/" + monthOfYear + "/" + dayOfMonth;
|
// datePicker.init(year, monthOfYear, dayOfMonth, new DatePicker.OnDateChangedListener() {
|
// @Override
|
// public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
|
// date = year + "/" + (monthOfYear + 1) + "/" + dayOfMonth;
|
// }
|
// });
|
// tv_cancel.setOnClickListener(new View.OnClickListener() {
|
// @Override
|
// public void onClick(View v) {
|
// alertDialog.dismiss();
|
// }
|
// });
|
// tv_confirm.setOnClickListener(new View.OnClickListener() {
|
// @Override
|
// public void onClick(View v) {
|
// tv_birthday.setText(date);
|
// alertDialog.dismiss();
|
// }
|
// });
|
}
|
}
|