package com.hanju.video.app.ui.login;
|
|
import android.annotation.SuppressLint;
|
import android.app.Activity;
|
import android.app.AlertDialog;
|
import android.content.Context;
|
import android.content.DialogInterface;
|
import android.content.Intent;
|
import android.content.SharedPreferences;
|
import android.graphics.Bitmap;
|
import android.graphics.BitmapFactory;
|
import android.os.Bundle;
|
import android.util.Base64;
|
import android.util.Log;
|
import android.view.LayoutInflater;
|
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 com.bumptech.glide.Glide;
|
import com.lcjian.library.util.Environment;
|
import com.lcjian.library.util.SingleToast;
|
import com.lcjian.library.util.common.StringUtils;
|
import com.hanju.video.app.BasicTextHttpResponseHandler;
|
import com.hanju.video.app.HttpApiUtil;
|
import com.hanju.video.app.ui.BaseActivity;
|
import com.hanju.video.app.ui.dialog.InputTextDialog;
|
import com.hanju.video.app.util.HanJuConstant;
|
import com.hanju.video.app.util.GlideCircleTransform;
|
import com.hanju.video.app.util.SelectPicUtil;
|
import com.hanju.video.app.R;
|
import com.hanju.video.app.util.ui.StatusBarUtil;
|
import com.hanju.video.app.util.ui.TopBarUtil;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayOutputStream;
|
import java.io.File;
|
import java.io.FileNotFoundException;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.util.Calendar;
|
|
/**
|
* Created by weikou2015 on 2016/10/27.
|
*/
|
|
public class PersonInfoActivity extends BaseActivity implements View.OnClickListener {
|
|
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;
|
|
@Override
|
protected void onCreate(Bundle arg0) {
|
super.onCreate(arg0);
|
setContentView(R.layout.activity_person_info);
|
StatusBarUtil.init(this);
|
TopBarUtil.init(this, "个人信息", this);
|
|
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);
|
|
ll_portrait.setOnClickListener(this);
|
ll_nickName.setOnClickListener(this);
|
ll_birthday.setOnClickListener(this);
|
ll_sex.setOnClickListener(this);
|
ll_autograph.setOnClickListener(this);
|
getPersonInfo();
|
}
|
|
private String sign = "";
|
|
private void getPersonInfo() {
|
SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
|
String loginUid = sp.getString("LoginUid", "");
|
String uid = sp.getString("uid", "");
|
HttpApiUtil.getPersonInfo(this, uid, loginUid, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (jsonObject.optBoolean("IsPost")) {
|
Glide.with(PersonInfoActivity.this).load(HanJuConstant.addPreFix(jsonObject.optJSONObject("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(jsonObject.optJSONObject("Data").optString("Nickname"));
|
tv_birthday.setText(jsonObject.optJSONObject("Data").optString("Birthday"));
|
tv_sex.setText(Integer.parseInt(jsonObject.optJSONObject("Data").optString("Sex")) == 0 ? "女" : "男");
|
sign = jsonObject.optJSONObject("Data").optString("Sign");
|
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) {
|
Bitmap bm = null;
|
bm = SelectPicUtil.onActivityResult(this, requestCode,
|
resultCode, data, imgWidth, imgHeight, imgWidth,
|
imgHeight);
|
|
if (bm != null) {
|
if (bm.getByteCount() / 1024 > maxSize) {
|
bm = compressImage(bm);
|
}
|
String path = saveBitmapTofile(bm);
|
final File file = new File(path);
|
if (!file.exists()) {
|
return;
|
}
|
imgBase64 = bitmapToBase64(bm);
|
Log.i("mresult", "图片路劲为:" + path + "----requestcode:" + requestCode);
|
Glide.with(PersonInfoActivity.this).load(file).transform(new GlideCircleTransform(PersonInfoActivity.this)).into(iv_portrait);
|
|
}
|
}
|
}
|
|
private String bitmapToBase64(Bitmap bitmap) {
|
|
String result = null;
|
ByteArrayOutputStream baos = null;
|
try {
|
if (bitmap != null) {
|
baos = new ByteArrayOutputStream();
|
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
|
|
baos.flush();
|
baos.close();
|
|
byte[] bitmapBytes = baos.toByteArray();
|
result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
try {
|
if (baos != null) {
|
baos.flush();
|
baos.close();
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return result;
|
}
|
|
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;
|
}
|
|
@Override
|
public void onClick(View v) {
|
switch (v.getId()) {
|
case R.id.tv_top_bar_left:
|
finish();
|
break;
|
case R.id.ll_portrait_select:
|
SelectPicUtil.getByAlbum(PersonInfoActivity.this);
|
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;
|
}
|
}
|
|
/**
|
* 上传个人信息
|
*/
|
private void upLoadRegisterData() {
|
SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
|
String uid = sp.getString("uid", "");
|
String loginUid = sp.getString("LoginUid", "");
|
HttpApiUtil.upLoadPersonInfo(this, uid, loginUid, 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() {
|
View v = LayoutInflater.from(this).inflate(R.layout.item_edit_dialog, null);
|
if (isFinishing())
|
return;
|
final AlertDialog alertDialog = new AlertDialog.Builder(PersonInfoActivity.this).setView(v).create();
|
alertDialog.show();
|
TextView tv_title = v.findViewById(R.id.tv_dialog_title);
|
tv_title.setText("个性签名");
|
final EditText et_msg = v.findViewById(R.id.et_dialog_message);
|
TextView tv_cancel = v.findViewById(R.id.tv_cancel);
|
TextView tv_confirm = v.findViewById(R.id.tv_confirm);
|
et_msg.setText(sign);
|
et_msg.setSelection(et_msg.getText().length());
|
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) {
|
String str = et_msg.getText().toString();
|
if (StringUtils.isEmpty(str)) {
|
SingleToast.showToast(PersonInfoActivity.this, "个性签名不能为空");
|
return;
|
} else {
|
tv_autograph.setText(str);
|
alertDialog.dismiss();
|
}
|
}
|
});
|
|
}
|
|
private void getNickName() {
|
final EditText et_msg = new EditText(this);
|
if (isFinishing())
|
return;
|
|
new InputTextDialog.Builder(this).setData("测试").setTitle("修改昵称").setFinishListener(new InputTextDialog.IEditFinishListener() {
|
@Override
|
public void cancel(DialogInterface dialog) {
|
dialog.dismiss();
|
}
|
|
@Override
|
public void finish(DialogInterface dialog, String data) {
|
if (StringUtils.isEmpty(data)) {
|
SingleToast.showToast(PersonInfoActivity.this, "昵称不能为空");
|
} else {
|
tv_nickName.setText(data);
|
dialog.dismiss();
|
}
|
}
|
}).create().show();
|
}
|
|
/**
|
* 获取生日
|
*/
|
String date = "";
|
|
private void getDate() {
|
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();
|
}
|
});
|
}
|
}
|