From 6aed6290ad05fe2aa125bb8e804e2e8c2f13a613 Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期二, 09 三月 2021 16:53:04 +0800 Subject: [PATCH] 我的里面信息修改修复,添加umeng事件统计,优化搜索结果页广告 --- BuWanVideo/src/com/weikou/beibeivideo/ui/login/PersonInfoActivity.java | 429 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 358 insertions(+), 71 deletions(-) diff --git a/BuWanVideo/src/com/weikou/beibeivideo/ui/login/PersonInfoActivity.java b/BuWanVideo/src/com/weikou/beibeivideo/ui/login/PersonInfoActivity.java index 68aac8c..625faf5 100644 --- a/BuWanVideo/src/com/weikou/beibeivideo/ui/login/PersonInfoActivity.java +++ b/BuWanVideo/src/com/weikou/beibeivideo/ui/login/PersonInfoActivity.java @@ -1,9 +1,10 @@ package com.weikou.beibeivideo.ui.login; +import android.Manifest; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; -import android.app.Dialog; +import android.content.ActivityNotFoundException; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; @@ -11,8 +12,11 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; +import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.provider.MediaStore; +import android.support.annotation.NonNull; import android.util.Base64; import android.util.Log; import android.view.LayoutInflater; @@ -28,6 +32,8 @@ import com.google.gson.Gson; import com.lcjian.library.dialog.DialogUtil; import com.lcjian.library.util.Environment; +import com.lcjian.library.permission.PermissionHelper; +import com.lcjian.library.permission.PermissionInterface; import com.lcjian.library.util.SingleToast; import com.lcjian.library.util.common.StringUtils; import com.tencent.mm.opensdk.modelmsg.SendAuth; @@ -41,8 +47,8 @@ import com.weikou.beibeivideo.ui.BaseActivity; import com.weikou.beibeivideo.ui.dialog.LoadingDialogUtil; import com.weikou.beibeivideo.util.BeibeiConstant; +import com.weikou.beibeivideo.util.FileUtils; import com.weikou.beibeivideo.util.GlideCircleTransform; -import com.weikou.beibeivideo.util.SelectPicUtil; import com.weikou.beibeivideo.R; import com.weikou.beibeivideo.util.UserUtil; @@ -52,9 +58,11 @@ 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 java.util.Map; @@ -62,7 +70,7 @@ * Created by weikou2015 on 2016/10/27. */ -public class PersonInfoActivity extends BaseActivity implements View.OnClickListener { +public class PersonInfoActivity extends BaseActivity implements View.OnClickListener, PermissionInterface { private String TAG = PersonInfoActivity.class.getSimpleName(); @@ -120,6 +128,7 @@ tv_bind_qq.setOnClickListener(this); tv_bind_wx.setOnClickListener(this); tv_bind_phone.setOnClickListener(this); + mPermissionsChecker = new PermissionHelper(this, this);//鏉冮檺 } @@ -212,64 +221,165 @@ } + + /** + * 鏉冮檺璇锋眰鐮� + * + * @return + */ + @Override + public int getPermissionsRequestCode() { + return 1001; + } + + /** + * 璇锋眰鏉冮檺 + * + * @return + */ + @Override + public String[] getPermissions() { + return new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}; + } + + /** + * 鏉冮檺璇锋眰鎴愬姛 + */ + @Override + public void requestPermissionsSuccess() { + ll_portrait.performClick(); + } + + /** + * 鏉冮檺璇锋眰澶辫触 + */ + @Override + public void requestPermissionsFail() { +// Toast.makeText(this, "浣犱互鎷掔粷鏉冮檺", Toast.LENGTH_SHORT).show(); + } + + /** + * 璇锋眰鏉冮檺缁撴灉 + * + * @param requestCode + * @param permissions + * @param grantResults + */ + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + if (mPermissionsChecker.requestPermissionsResult(requestCode, permissions, grantResults)) { + //鏉冮檺璇锋眰缁撴灉锛屽苟宸茬粡澶勭悊浜嗚鍥炶皟 + return; + } + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + } + + String imgBase64 = ""; + + private Uri uriClipUri;//鍓鍥剧墖璺緞 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); UMShareAPI.get(this).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); + //鍥惧簱 + if (resultCode == RESULT_OK && resultCode != RESULT_CANCELED && requestCode == 1001 && data != null) {//鐩稿唽 + Uri selectedImage = data.getData(); + startPhotoZoom(selectedImage);//绯荤粺鍓 + } else if (resultCode == RESULT_OK && requestCode == 1002 && data != null) {//鍓瀹屾垚 + if (uriClipUri != null) { + //鍥惧簱 + Photo_crop crop = new Photo_crop();//鍒濆鍖� 鍥剧墖鍘嬬缉 + File f = new File(uriClipUri.getPath());//鑾峰彇鍥剧墖 + if (crop.compressPhto(f, false)) {//鍘嬬缉骞朵繚瀛樻垚鍔� + Uri uri = Uri.parse("file://" + "/" + android.os.Environment.getExternalStorageDirectory().getPath() + "/YSDQImg/" + "avatar.png"); + f = new File(uri.getPath()); + 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); + //涓婁紶 + updatePersonInfo(null, null, null, imgBase64, null); } - 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); - //涓婁紶 - updatePersonInfo(null, null, null, imgBase64, null); } } } - private String bitmapToBase64(Bitmap bitmap) { + /** + * 鑾峰彇鐩稿唽鍥剧墖 + * + * @param uri + */ + public void startPhotoZoom(Uri uri) { + File storageDir = new File(android.os.Environment.getExternalStorageDirectory().getPath() + "/YSDQImg/"); + if (!storageDir.exists()) {//娌℃湁鏂囦欢澶瑰垯鍒涘缓 + storageDir.mkdir(); + } + //com.android.camera.action.CROP锛岃繖涓猘ction鏄皟鐢ㄧ郴缁熻嚜甯︾殑鍥剧墖瑁佸垏鍔熻兘 + Intent intent = new Intent("com.android.camera.action.CROP"); + intent.setDataAndType(uri, "image/*");//瑁佸壀鐨勫浘鐗噓ri鍜屽浘鐗囩被鍨� + intent.putExtra("crop", "true");//璁剧疆鍏佽瑁佸壀锛屽鏋滀笉璁剧疆锛屽氨浼氳烦杩囪鍓殑杩囩▼锛岃繕鍙互璁剧疆putExtra("crop", "circle") + if (Build.MANUFACTURER.equals("HUAWEI")) { //鍗庝负鐗规畩澶勭悊 涓嶇劧浼氭樉绀哄渾 + intent.putExtra("aspectX", 9998); + intent.putExtra("aspectY", 9999); + } else { + intent.putExtra("aspectX", 1);//瑁佸壀妗嗙殑 X 鏂瑰悜鐨勬瘮渚�,闇�瑕佷负鏁存暟 + intent.putExtra("aspectY", 1);//瑁佸壀妗嗙殑 Y 鏂瑰悜鐨勬瘮渚�,闇�瑕佷负鏁存暟 + } + intent.putExtra("outputX", 250);//杩斿洖鏁版嵁鐨勬椂鍊欑殑X鍍忕礌澶у皬銆� + intent.putExtra("outputY", 250);//杩斿洖鏁版嵁鐨勬椂鍊欑殑Y鍍忕礌澶у皬銆� + //瑁佸壀鏃舵槸鍚︿繚鐣欏浘鐗囩殑姣斾緥锛岃繖閲岀殑姣斾緥鏄�1:1 + intent.putExtra("scale", true); + //鏄惁鏄渾褰㈣鍓尯鍩燂紝璁剧疆浜嗕篃涓嶄竴瀹氭湁鏁� + intent.putExtra("circleCrop", false); - String result = null; - ByteArrayOutputStream baos = null; + //uritempFile涓篣ri绫诲彉閲忥紝瀹炰緥鍖杣ritempFile + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + //濡傛灉鏄�7.0鐨勭浉鍐� + //璁剧疆瑁佸壀鐨勫浘鐗囧湴鍧�Uri + uriClipUri = Uri.parse("file://" + "/" + android.os.Environment.getExternalStorageDirectory().getPath() + "/YSDQImg/" + System.currentTimeMillis() + "_clip.jpg"); + } else { + uriClipUri = Uri.parse("file://" + "/" + android.os.Environment.getExternalStorageDirectory().getPath() + "/YSDQImg/" + System.currentTimeMillis() + "_clip.jpg"); + } + //Android 瀵笽ntent涓墍鍖呭惈鏁版嵁鐨勫ぇ灏忔槸鏈夐檺鍒剁殑锛屼竴鑸笉鑳借秴杩� 1M锛屽惁鍒欎細浣跨敤缂╃暐鍥� ,鎵�浠ユ垜浠鎸囧畾杈撳嚭瑁佸壀鐨勫浘鐗囪矾寰� + intent.putExtra(MediaStore.EXTRA_OUTPUT, uriClipUri); + intent.putExtra("return-data", false);//鏄惁灏嗘暟鎹繚鐣欏湪Bitmap涓繑鍥� + intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//杈撳嚭鏍煎紡锛屼竴鑸涓築itmap鏍煎紡鍙婂浘鐗囩被鍨� + intent.putExtra("noFaceDetection", false);//浜鸿劯璇嗗埆鍔熻兘 + startActivityForResult(intent, 1002);//瑁佸壀瀹屾垚鐨勬爣璇� + } + + private String bitmapToBase64(File file) { + String base64 = null; + InputStream in = 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); - } + in = new FileInputStream(file); + byte[] bytes = new byte[in.available()]; + int length = in.read(bytes); + base64 = Base64.encodeToString(bytes, 0, length, Base64.DEFAULT); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } catch (IOException e) { + // TODO Auto-generated catch block e.printStackTrace(); } finally { try { - if (baos != null) { - baos.flush(); - baos.close(); + if (in != null) { + in.close(); } } catch (IOException e) { + // TODO Auto-generated catch block e.printStackTrace(); } } - return result; + return base64; + } private String saveBitmapTofile(Bitmap bm) { @@ -318,6 +428,8 @@ return bitmap; } + private PermissionHelper mPermissionsChecker; // 鏉冮檺妫�娴嬪櫒 妫�娴嬫槸鍚︽湁鍌ㄥ瓨鍗″啓鍏ヨ鍙栨潈闄� + @Override public void onClick(View v) { switch (v.getId()) { @@ -325,7 +437,16 @@ finish(); break; case R.id.ll_portrait_select: - SelectPicUtil.getByAlbum(PersonInfoActivity.this); + if (mPermissionsChecker.lacksPermissions(getPermissions())) {//妫�娴嬫槸鍚︽湁鍐欏叆鐨勬潈闄� + mPermissionsChecker.showNormalDialog(); + } else { + Intent picture = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); + try { + startActivityForResult(picture, 1001); + } catch (ActivityNotFoundException exception) { + Toast.makeText(this, "no activity", Toast.LENGTH_SHORT).show(); + } + } break; case R.id.ll_nickName: getNickName(); @@ -427,6 +548,20 @@ String loginUid = UserUtil.getLoginUid(this); BeibeiVideoAPI.upLoadPersonInfo(this, uid, loginUid, sex, birthday, personSign, portrait, nickName, new BasicTextHttpResponseHandler() { + + @Override + public void onStart() { + super.onStart(); + if (portrait != null) + DialogUtil.show(loadingDialog); + } + + @Override + public void onFinish() { + super.onFinish(); + DialogUtil.dismiss(loadingDialog); + } + @Override public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception { if (jsonObject.optBoolean("IsPost")) { @@ -471,27 +606,23 @@ 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 = (TextView) v.findViewById(R.id.tv_dialog_title); tv_title.setText("涓�х鍚�"); + tv_title.setVisibility(View.GONE); final EditText et_msg = (EditText) v.findViewById(R.id.et_dialog_message); TextView tv_cancel = (TextView) v.findViewById(R.id.tv_cancel); TextView tv_confirm = (TextView) 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() { + if (isFinishing()) + return; + final AlertDialog alertDialog = new AlertDialog.Builder(PersonInfoActivity.this).setTitle("涓�х鍚�").setView(v).setNegativeButton("鍙栨秷", new DialogInterface.OnClickListener() { @Override - public void onClick(View v) { + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }).setPositiveButton("纭畾", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { String str = et_msg.getText().toString(); if (StringUtils.isEmpty(str)) { SingleToast.showToast(PersonInfoActivity.this, "涓�х鍚嶄笉鑳戒负绌�"); @@ -499,17 +630,24 @@ } else { tv_autograph.setText(str); updatePersonInfo(null, null, str, null, null); - alertDialog.dismiss(); + dialog.dismiss(); } } - }); - + }).create(); + alertDialog.show(); + et_msg.setText(sign); + et_msg.setSelection(et_msg.getText().length()); } private void getNickName() { final EditText et_msg = new EditText(this); + et_msg.setBackground(null); if (isFinishing()) return; + if (tv_nickName.getText() != null) { + et_msg.setText(tv_nickName.getText() + ""); + et_msg.setSelection(tv_nickName.getText().length()); + } AlertDialog alertDialog = new AlertDialog.Builder(PersonInfoActivity.this).setView(et_msg).setTitle("鏄电О淇敼") .setPositiveButton("纭畾", new DialogInterface.OnClickListener() { @Override @@ -541,7 +679,19 @@ 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(); + final AlertDialog alertDialog = new AlertDialog.Builder(PersonInfoActivity.this).setView(view).setNegativeButton("鍙栨秷", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }).setPositiveButton("纭畾", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + tv_birthday.setText(date); + updatePersonInfo(null, date, null, null, null); + dialog.dismiss(); + } + }).setTitle("鐢熸棩閫夋嫨").create(); alertDialog.show(); DatePicker datePicker = (DatePicker) view.findViewById(R.id.datePicker); TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel); @@ -558,20 +708,7 @@ 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); - updatePersonInfo(null, date, null, null, null); - alertDialog.dismiss(); - } - }); + } @Override @@ -580,5 +717,155 @@ getPersonInfo(); } + @Override + protected void onDestroy() { + super.onDestroy(); + String filePath = android.os.Environment.getExternalStorageDirectory().getPath() + "/YSDQImg/"; + if (new File(filePath).exists()) { + FileUtils.deleteDirectory(filePath); + } + } + + class Photo_crop { + /** + * 鑾峰彇鐩稿唽鍥剧墖 + * + * @param uri + */ + public Uri startPhotoZoom(Uri uri, Context mContext) { + Uri uriClipUri = null; + //com.android.camera.action.CROP锛岃繖涓猘ction鏄皟鐢ㄧ郴缁熻嚜甯︾殑鍥剧墖瑁佸垏鍔熻兘 + Intent intent = new Intent("com.android.camera.action.CROP"); + intent.setDataAndType(uri, "image/*");//瑁佸壀鐨勫浘鐗噓ri鍜屽浘鐗囩被鍨� + intent.putExtra("crop", "true");//璁剧疆鍏佽瑁佸壀锛屽鏋滀笉璁剧疆锛屽氨浼氳烦杩囪鍓殑杩囩▼锛岃繕鍙互璁剧疆putExtra("crop", "circle") + if (Build.MANUFACTURER.equals("HUAWEI")) { //鍗庝负鐗规畩澶勭悊 涓嶇劧浼氭樉绀哄渾 + intent.putExtra("aspectX", 9998); + intent.putExtra("aspectY", 9999); + } else { + intent.putExtra("aspectX", 1);//瑁佸壀妗嗙殑 X 鏂瑰悜鐨勬瘮渚�,闇�瑕佷负鏁存暟 + intent.putExtra("aspectY", 1);//瑁佸壀妗嗙殑 Y 鏂瑰悜鐨勬瘮渚�,闇�瑕佷负鏁存暟 + } + intent.putExtra("outputX", 600);//杩斿洖鏁版嵁鐨勬椂鍊欑殑X鍍忕礌澶у皬銆� + intent.putExtra("outputY", 600);//杩斿洖鏁版嵁鐨勬椂鍊欑殑Y鍍忕礌澶у皬銆� + //瑁佸壀鏃舵槸鍚︿繚鐣欏浘鐗囩殑姣斾緥锛岃繖閲岀殑姣斾緥鏄�1:1 + intent.putExtra("scale", true); + //鏄惁鏄渾褰㈣鍓尯鍩焧rue锛岃缃簡涔熶笉涓�瀹氭湁鏁� + intent.putExtra("circleCrop", false); + //uritempFile涓篣ri绫诲彉閲忥紝瀹炰緥鍖杣ritempFile + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + //濡傛灉鏄�7.0鐨勭浉鍐� + //璁剧疆瑁佸壀鐨勫浘鐗囧湴鍧�Uri + uriClipUri = Uri.parse("file://" + "/" + android.os.Environment.getExternalStorageDirectory().getPath() + "/YSDQImg/" + "clip.jpg"); + } else { + uriClipUri = Uri.parse("file://" + "/" + android.os.Environment.getExternalStorageDirectory().getPath() + "/YSDQImg/" + "clip.jpg"); + } + Log.e("uriClipUri=====", "" + uriClipUri); + //Android 瀵笽ntent涓墍鍖呭惈鏁版嵁鐨勫ぇ灏忔槸鏈夐檺鍒剁殑锛屼竴鑸笉鑳借秴杩� 1M锛屽惁鍒欎細浣跨敤缂╃暐鍥� ,鎵�浠ユ垜浠鎸囧畾杈撳嚭瑁佸壀鐨勫浘鐗囪矾寰� + intent.putExtra(MediaStore.EXTRA_OUTPUT, uriClipUri); + intent.putExtra("return-data", false);//鏄惁灏嗘暟鎹繚鐣欏湪Bitmap涓繑鍥� + intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//杈撳嚭鏍煎紡锛屼竴鑸涓築itmap鏍煎紡鍙婂浘鐗囩被鍨� + intent.putExtra("noFaceDetection", false);//浜鸿劯璇嗗埆鍔熻兘 + ((Activity) mContext).startActivityForResult(intent, 1002);//瑁佸壀瀹屾垚鐨勬爣璇� + return uriClipUri; + } + + /** + * 鍥剧墖鍘嬬缉鐨勬柟娉�(鍙槸鍐呭瓨鍑忓皯锛岄伩鍏峯om锛屽浘鐗囨湰韬湪disk鐩樹綋绉笉鍙�) + * 鏄剧ず鐨凚itmap鍗犵敤鐨勫唴瀛樺皯涓�鐐癸紝杩樻槸闇�瑕佸幓璁剧疆鍔犺浇鐨勫儚绱犻暱搴﹀拰瀹藉害锛堝彉鎴愮缉鐣ュ浘锛� + * + * @param mFile + * @param crop 鏄惁鍓鍦嗗舰鍥剧墖 true 鏄� false 鍚� + * @return + */ + public boolean compressPhto(File mFile, boolean crop) { +// BitmapFactory杩欎釜绫诲氨鎻愪緵浜嗗涓В鏋愭柟娉曪紙decodeResource銆乨ecodeStream銆乨ecodeFile绛夛級鐢ㄤ簬鍒涘缓Bitmap銆� +// 姣斿濡傛灉鍥剧墖鏉ユ簮浜庣綉缁滐紝灏卞彲浠ヤ娇鐢╠ecodeStream鏂规硶锛� +// 濡傛灉鏄痵d鍗¢噷闈㈢殑鍥剧墖锛屽氨鍙互閫夋嫨decodeFile鏂规硶锛� +// 濡傛灉鏄祫婧愭枃浠堕噷闈㈢殑鍥剧墖锛屽氨鍙互浣跨敤decodeResource鏂规硶绛� + BitmapFactory.Options options = new BitmapFactory.Options(); + options.inJustDecodeBounds = true; // 鑾峰彇褰撳墠鍥剧墖鐨勮竟鐣屽ぇ灏� + //BitmapFactory.decodeResource(getResources(), R.drawable.bg, options); + BitmapFactory.decodeFile(mFile.getAbsolutePath(), options); + int outHeight = options.outHeight; //鑾峰彇鍥剧墖鏈韩鐨勯珮鍍忕礌 + int outWidth = options.outWidth;//鑾峰彇鍥剧墖鏈韩鐨勫鐨勫儚绱� + String outMimeType = options.outMimeType; + options.inJustDecodeBounds = false; + //inSampleSize鐨勪綔鐢ㄥ氨鏄彲浠ユ妸鍥剧墖鐨勯暱鐭缉灏廼nSampleSize鍊嶏紝鎵�鍗犲唴瀛樼缉灏廼nSampleSize鐨勫钩鏂� + //瀵逛簬inSampleSize鍊肩殑澶у皬鏈夎姹傦紝鏈�濂芥槸鏁存暟涓�2鐨勫�嶆暟 + options.inSampleSize = caculateSampleSize(options, 500, 500); + //etPath()寰楀埌鐨勬槸鏋勯�爁ile鐨勬椂鍊欑殑璺緞銆俫etAbsolutePath()寰楀埌鐨勬槸鍏ㄨ矾寰� + String path = mFile.getPath(); + String absPath = mFile.getAbsolutePath(); + Bitmap bitmap = BitmapFactory.decodeFile(absPath, options); + try { + return saveBitmap(bitmap, "avatar.png", crop);//淇濆瓨鎴愬姛 + } catch (IOException e) { + e.printStackTrace(); + return false; + } +// ivUserPhoto.setImageBitmap(bitmap); +// //灏哄鍘嬬缉缁撴灉 +// ivSize.setImageBitmap(bitmap); + } + + /** + * 璁$畻鍑烘墍闇�瑕佸帇缂╃殑澶у皬 + * + * @param options + * @param reqWidth 甯屾湜鐨勫浘鐗囧澶у皬 + * @param reqHeight 甯屾湜鐨勫浘鐗囬珮澶у皬 + * @return + */ + private int caculateSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { + int sampleSize = 1; + int picWidth = options.outWidth; + int picHeight = options.outHeight; + if (picWidth > reqWidth || picHeight > reqHeight) { + int halfPicWidth = picWidth / 2; + int halfPicHeight = picHeight / 2; + while (halfPicWidth / sampleSize > reqWidth || halfPicHeight / sampleSize > reqHeight) { + sampleSize *= 2; + } + } + return sampleSize; + } + + + /** + * 淇濆瓨鏂囦欢锛� + * + * @param bitmap + * @param bitName 鏂囦欢鍚� + * @param crop + * @return + * @throws IOException + */ + private boolean saveBitmap(Bitmap bitmap, String bitName, boolean crop) throws IOException { + boolean save = false; + File storageDir = new File(Environment.getExternalStorageDirectory().getPath() + "/YSDQImg/"); + if (!storageDir.exists()) {//娌℃湁鏂囦欢澶瑰垯鍒涘缓 + storageDir.mkdir(); + } + String fileName; + File file; + fileName = Environment.getExternalStorageDirectory().getPath() + "/YSDQImg/" + bitName; + file = new File(fileName); + if (bitmap != null) { + if (file.exists()) { + file.delete(); + } + FileOutputStream out; + + out = new FileOutputStream(file); + // 鏍煎紡涓� JPEG锛岀収鐩告満鎷嶅嚭鐨勫浘鐗囦负JPEG鏍煎紡鐨勶紝PNG鏍煎紡鐨勪笉鑳芥樉绀哄湪鐩稿唽涓� + if (bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out)) { + out.flush(); + out.close(); + } + save = true;//淇濆瓨鎴愬姛 + } + return save; + } + } } -- Gitblit v1.8.0