| | |
| | | import android.os.Bundle; |
| | | import android.provider.MediaStore; |
| | | import androidx.annotation.NonNull; |
| | | import androidx.core.content.FileProvider; |
| | | |
| | | import android.util.Base64; |
| | | import android.util.Log; |
| | | import android.view.LayoutInflater; |
| | |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.nio.file.Files; |
| | | import java.util.Calendar; |
| | | import java.util.Map; |
| | | |
| | |
| | | public class PersonInfoActivity extends BaseActivity implements View.OnClickListener, PermissionInterface { |
| | | |
| | | private String TAG = PersonInfoActivity.class.getSimpleName(); |
| | | private String BASE_FILE_DIR = Environment.getExternalStorageDirectory().getPath() + "/Download/YSDQImg"; |
| | | |
| | | private LinearLayout ll_portrait; |
| | | private LinearLayout ll_nickName; |
| | |
| | | @Override |
| | | protected void onCreate(Bundle arg0) { |
| | | super.onCreate(arg0); |
| | | // BASE_FILE_DIR = getCacheDir().getAbsolutePath(); |
| | | setContentView(R.layout.activity_person_info); |
| | | TopStatusSettings.setStatusViewAndDeepColor(this); |
| | | ll_portrait = (LinearLayout) findViewById(R.id.ll_portrait_select); |
| | |
| | | //图库 |
| | | 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"); |
| | | if(!f.exists()){ |
| | | Log.i(TAG,"裁剪的图片不存在"); |
| | | } |
| | | String targetFilePath = BASE_FILE_DIR + "/avatar.png"; |
| | | if (crop.compressPhto(f, targetFilePath, false)) {//压缩并保存成功 |
| | | Uri uri = Uri.parse("file://" + "/" + targetFilePath); |
| | | f = new File(uri.getPath()); |
| | | imgBase64 = bitmapToBase64(f); |
| | | iv_portrait.postDelayed(new Runnable() { |
| | |
| | | * @param uri |
| | | */ |
| | | public void startPhotoZoom(Uri uri) { |
| | | File storageDir = new File(android.os.Environment.getExternalStorageDirectory().getPath() + "/YSDQImg/"); |
| | | File storageDir = new File(BASE_FILE_DIR); |
| | | if (!storageDir.exists()) {//没有文件夹则创建 |
| | | storageDir.mkdir(); |
| | | storageDir.mkdirs(); |
| | | } |
| | | //com.android.camera.action.CROP,这个action是调用系统自带的图片裁切功能 |
| | | Intent intent = new Intent("com.android.camera.action.CROP"); |
| | |
| | | |
| | | //uritempFile为Uri类变量,实例化uritempFile |
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
| | | |
| | | File file = new File(BASE_FILE_DIR+"/"+ System.currentTimeMillis() + "_clip.jpg"); |
| | | // uriClipUri = FileProvider.getUriForFile(getApplicationContext(),getApplicationContext().getPackageName() + ".fileprovider",file); |
| | | |
| | | //如果是7.0的相册 |
| | | //设置裁剪的图片地址Uri |
| | | uriClipUri = Uri.parse("file://" + "/" + android.os.Environment.getExternalStorageDirectory().getPath() + "/YSDQImg/" + System.currentTimeMillis() + "_clip.jpg"); |
| | | uriClipUri = Uri.parse("file://" + "/" + BASE_FILE_DIR + "/"+ System.currentTimeMillis() + "_clip.jpg"); |
| | | } else { |
| | | uriClipUri = Uri.parse("file://" + "/" + android.os.Environment.getExternalStorageDirectory().getPath() + "/YSDQImg/" + System.currentTimeMillis() + "_clip.jpg"); |
| | | uriClipUri = Uri.parse("file://" + "/" + BASE_FILE_DIR + "/" + System.currentTimeMillis() + "_clip.jpg"); |
| | | } |
| | | //Android 对Intent中所包含数据的大小是有限制的,一般不能超过 1M,否则会使用缩略图 ,所以我们要指定输出裁剪的图片路径 |
| | | intent.putExtra(MediaStore.EXTRA_OUTPUT, uriClipUri); |
| | |
| | | @Override |
| | | protected void onDestroy() { |
| | | super.onDestroy(); |
| | | String filePath = android.os.Environment.getExternalStorageDirectory().getPath() + "/YSDQImg/"; |
| | | String filePath = BASE_FILE_DIR; |
| | | 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,这个action是调用系统自带的图片裁切功能 |
| | | Intent intent = new Intent("com.android.camera.action.CROP"); |
| | | intent.setDataAndType(uri, "image/*");//裁剪的图片uri和图片类型 |
| | | 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); |
| | | //是否是圆形裁剪区域true,设置了也不一定有效 |
| | | intent.putExtra("circleCrop", false); |
| | | //uritempFile为Uri类变量,实例化uritempFile |
| | | 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 对Intent中所包含数据的大小是有限制的,一般不能超过 1M,否则会使用缩略图 ,所以我们要指定输出裁剪的图片路径 |
| | | intent.putExtra(MediaStore.EXTRA_OUTPUT, uriClipUri); |
| | | intent.putExtra("return-data", false);//是否将数据保留在Bitmap中返回 |
| | | intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//输出格式,一般设为Bitmap格式及图片类型 |
| | | intent.putExtra("noFaceDetection", false);//人脸识别功能 |
| | | ((Activity) mContext).startActivityForResult(intent, 1002);//裁剪完成的标识 |
| | | return uriClipUri; |
| | | } |
| | | |
| | | /** |
| | | * 图片压缩的方法(只是内存减少,避免oom,图片本身在disk盘体积不变) |
| | | * 显示的Bitmap占用的内存少一点,还是需要去设置加载的像素长度和宽度(变成缩略图) |
| | |
| | | * @param crop 是否剪裁圆形图片 true 是 false 否 |
| | | * @return |
| | | */ |
| | | public boolean compressPhto(File mFile, boolean crop) { |
| | | public boolean compressPhto(File mFile,String targetFilePath, boolean crop) { |
| | | // BitmapFactory这个类就提供了多个解析方法(decodeResource、decodeStream、decodeFile等)用于创建Bitmap。 |
| | | // 比如如果图片来源于网络,就可以使用decodeStream方法; |
| | | // 如果是sd卡里面的图片,就可以选择decodeFile方法; |
| | |
| | | String path = mFile.getPath(); |
| | | String absPath = mFile.getAbsolutePath(); |
| | | Bitmap bitmap = BitmapFactory.decodeFile(absPath, options); |
| | | |
| | | try { |
| | | return saveBitmap(bitmap, "avatar.png", crop);//保存成功 |
| | | if(bitmap==null){ |
| | | bitmap = BitmapFactory.decodeStream(Files.newInputStream(mFile.toPath())); |
| | | } |
| | | return saveBitmap(bitmap, targetFilePath, crop);//保存成功 |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | return false; |
| | |
| | | * 保存文件, |
| | | * |
| | | * @param bitmap |
| | | * @param bitName 文件名 |
| | | * @param path 文件路径 |
| | | * @param crop |
| | | * @return |
| | | * @throws IOException |
| | | */ |
| | | private boolean saveBitmap(Bitmap bitmap, String bitName, boolean crop) throws IOException { |
| | | private boolean saveBitmap(Bitmap bitmap, String path, boolean crop) throws IOException { |
| | | boolean save = false; |
| | | File storageDir = new File(Environment.getExternalStorageDirectory().getPath() + "/YSDQImg/"); |
| | | File storageDir = new File(BASE_FILE_DIR); |
| | | if (!storageDir.exists()) {//没有文件夹则创建 |
| | | storageDir.mkdir(); |
| | | } |
| | | String fileName; |
| | | File file; |
| | | fileName = Environment.getExternalStorageDirectory().getPath() + "/YSDQImg/" + bitName; |
| | | fileName = path; |
| | | file = new File(fileName); |
| | | if (bitmap != null) { |
| | | if (file.exists()) { |
| | | file.delete(); |
| | | } |
| | | // 创建上级文件夹 |
| | | if(!file.getParentFile().exists()) |
| | | { |
| | | file.getParentFile().mkdirs(); |
| | | } |
| | | // 创建文件 |
| | | if(!file.exists()){ |
| | | file.createNewFile(); |
| | | } |
| | | FileOutputStream out; |
| | | |
| | | out = new FileOutputStream(file); |
| | | // 格式为 JPEG,照相机拍出的图片为JPEG格式的,PNG格式的不能显示在相册中 |
| | | if (bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out)) { |