<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:onClick="fanHuiMy"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:background="@drawable/leftjiantou"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="25sp"
android:text="个人信息"/>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#3333"/>
<RelativeLayout
android:onClick="geRenXingXi"
android:layout_width="match_parent"
android:layout_height="120dp">
<TextView
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="头像"/>
<ImageView
android:id="@+id/image_tou"
android:layout_toLeftOf="@+id/img"
android:layout_marginRight="20dp"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:background="@drawable/user"/>
public class GeRenXinXiActivity extends AppCompatActivity implements View.OnClickListener {
private static final int PHOTO_REQUEST_CAREMA = 1;// 拍照
private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
private static final int PHOTO_REQUEST_CUT = 3;// 结果
/* 头像名称 */
private static final String PHOTO_FILE_NAME = "temp_photo.jpg";
private File tempFile;
private TextView text_paizhao;
private TextView text_bengdi;
private TextView text_quxiao;
private PopupWindow popupWindow;
private ImageView image_tou;
private SharedPreferences userInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ge_ren_xin_xi);
image_tou = findViewById(R.id.image_tou);
//用户uid
userInfo = Frag_My.userInfo;
getBitmapFromSharedPreferences();
}
public void fanHuiMy(View view) {
finish();
}
public void geRenXingXi(View view) {
View inflate = View.inflate(GeRenXinXiActivity
.this, R.layout.popup_window, null);
View inflate1 = View.inflate(GeRenXinXiActivity
.this, R.layout.activity_ge_ren_xin_xi, null);
popupWindow = new PopupWindow(inflate, ActionBar
.LayoutParams.MATCH_PARENT,ActionBar.LayoutParams
.WRAP_CONTENT,true);
popupWindow.setTouchable(true);
text_paizhao = inflate.findViewById(R.id.text_paizhao);
text_bengdi = inflate.findViewById(R.id.text_bengdi);
text_quxiao = inflate.findViewById(R.id.text_quxiao);
text_paizhao.setOnClickListener(this);
text_bengdi.setOnClickListener(this);
text_quxiao.setOnClickListener(this);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(
new BitmapDrawable());
popupWindow.showAtLocation(inflate, Gravity.BOTTOM,0,0);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.text_paizhao:
// 激活相机
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
// 判断存储卡是否可以用,可用进行存储
if (hasSdcard()) {
tempFile = new File(Environment.getExternalStorageDirectory()
, PHOTO_FILE_NAME);
// 从文件中创建uri
Uri uri = Uri.fromFile(tempFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
}
// 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CAREMA
startActivityForResult(intent, PHOTO_REQUEST_CAREMA);
break;
case R.id.text_bengdi:
// 激活系统图库,选择一张图片
Intent intent1 = new Intent(Intent.ACTION_PICK);
intent1.setType("image/*");
// 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_GALLERY
startActivityForResult(intent1, PHOTO_REQUEST_GALLERY);
break;
case R.id.text_quxiao:
popupWindow.dismiss();
break;
}
}
/* * 判断sdcard是否被挂载 */
private boolean hasSdcard() {
//判断SD卡手否是安装好的 media_mounted
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
/* * 剪切图片 */
private void crop(Uri uri) {
// 裁剪图片意图
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// 裁剪框的比例,1:1
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// 裁剪后输出图片的尺寸大小
intent.putExtra("outputX", 250);
intent.putExtra("outputY", 250);
intent.putExtra("outputFormat", "JPEG");
// 图片格式
intent.putExtra("noFaceDetection", true);
// 取消人脸识别
intent.putExtra("return-data", true);
// 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CUT
startActivityForResult(intent, PHOTO_REQUEST_CUT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PHOTO_REQUEST_GALLERY) {
// 从相册返回的数据
if (data != null) {
// 得到图片的全路径
Uri uri = data.getData();
crop(uri);
}
} else if (requestCode == PHOTO_REQUEST_CAREMA) {
// 从相机返回的数据
if (hasSdcard()) {
crop(Uri.fromFile(tempFile));
} else {
Toast.makeText(GeRenXinXiActivity.this, "未找到存储卡,无法存储照片!", Toast.LENGTH_SHORT).show();
}
} else if (requestCode == PHOTO_REQUEST_CUT) {
// 从剪切图片返回的数据
if (data != null) {
Bitmap bitmap = data.getParcelableExtra("data");
/** * 获得图片 */
image_tou.setImageBitmap(bitmap);
//保存到SharedPreferences
saveBitmapToSharedPreferences(bitmap);
}
try {
// 将临时文件删除
tempFile.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//保存图片到SharedPreferences
private void saveBitmapToSharedPreferences(Bitmap bitmap) {
// Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
// 第一步:将Bitmap压缩至字节数组输出流ByteArrayOutputStream
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 80, byteArrayOutputStream);
//第二步:利用Base64将字节数组输出流中的数据转换成字符串String
byte[] byteArray = byteArrayOutputStream.toByteArray();
String imageString = new String(Base64.encodeToString(byteArray, Base64.DEFAULT));
//第三步:将String保持至SharedPreferences
SharedPreferences sharedPreferences = getSharedPreferences("testSP", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("image", imageString);
editor.commit();
//上传头像
setImgByStr(imageString,"");
}
/**
* 上传头像
*/
public void setImgByStr(String imgStr, String imgName) {
String uid = userInfo.getString("uid", null);
//这里是头像接口,通过Post请求,拼接接口地址和ID,上传数据。
String url = "https://www.zhaoapi.cn/file/upload";
Map<String, String> params = new HashMap<String, String>();
params.put("uid", uid);
// 11111111
params.put("file", imgStr);
OkHttpUtil.doPost(url, params, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
}
});
}
//从SharedPreferences获取图片
private void getBitmapFromSharedPreferences(){
SharedPreferences sharedPreferences=getSharedPreferences("testSP"
, Context.MODE_PRIVATE);
//第一步:取出字符串形式的Bitmap
String imageString=sharedPreferences.getString("image", "");
//第二步:利用Base64将字符串转换为ByteArrayInputStream
byte[] byteArray= Base64.decode(imageString, Base64.DEFAULT);
if(byteArray.length==0){
}else{
ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(byteArray);
//第三步:利用ByteArrayInputStream生成Bitmap
Bitmap bitmap= BitmapFactory.decodeStream(byteArrayInputStream);
image_tou.setImageBitmap(bitmap);
}
}
}
//返回上一个(Activity)页面获取图片方法
SharedPreferences sharedPreferences=getSharedPreferences("testSP"
, Context.MODE_PRIVATE);
if (sharedPreferences!=null){
//第一步:取出字符串形式的Bitmap
String imageString= sharedPreferences.getString("image", "");
//第二步:利用Base64将字符串转换为ByteArrayInputStream
byte[] byteArray= Base64.decode(imageString, Base64.DEFAULT);
if(byteArray.length==0){
}else{
ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(byteArray);
//第三步:利用ByteArrayInputStream生成Bitmap
Bitmap bitmap= BitmapFactory.decodeStream(byteArrayInputStream);
img.setImageBitmap(bitmap);
}
}