..需要下载的工具类 https://share.weiyun.com/5wJW6VS
拍照,使用存储卡路径(需要申请存储权限),即图片的路径在 存储卡目录下 -> 包名 -> icon文件夹下
public void takePhoto(View view) { imgPath = FileUtils.generateImgePathInStoragePath(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //如果是6.0或6.0以上,则要申请运行时权限,这里需要申请拍照的权限 requestRuntimePermission(new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, new PermissionListener() { @Override public void onGranted() { //开启摄像头,拍照完的图片保存在对应路径下 openCamera(imgPath); //DoGet(); } @Override public void onDenied(List deniedPermissions) { UIUtils.showToast("所需权限被拒绝"); } }); return; } openCamera(imgPath);}复制代码
打开相册
public void openAlbum(View view) { Intent intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(intent, REQ_ALBUM);}复制代码
开启摄像机
private void openCamera(String imgPath) { // 指定调用相机拍照后照片的储存路径 File imgFile = new File(imgPath); Uri imgUri = null; if (Build.VERSION.SDK_INT >= 24) { //如果是7.0或以上 imgUri = FileProvider.getUriForFile(this, UIUtils.getPackageName() + ".fileprovider", imgFile); } else { imgUri = Uri.fromFile(imgFile); } Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri); startActivityForResult(intent, REQ_TAKE_PHOTO);}复制代码
申请运行时权限
public void requestRuntimePermission(String[] permissions, PermissionListener permissionListener) { this.permissionListener = permissionListener; List permissionList = new ArrayList<>(); for (String permission : permissions) { if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { permissionList.add(permission); } } if (!permissionList.isEmpty()) { ActivityCompat.requestPermissions(this, permissionList.toArray(new String[permissionList.size()]), 1); } else { permissionListener.onGranted(); }}复制代码
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (resultCode) { case RESULT_OK://调用图片选择处理成功 String zoomImgPath = ""; Bitmap bm = null; File temFile = null; File srcFile = null; File outPutFile = null; switch (requestCode) { case REQ_TAKE_PHOTO:// 拍照后在这里回调 srcFile = new File(imgPath); outPutFile = new File(FileUtils.generateImgePathInStoragePath()); outputUri = Uri.fromFile(outPutFile); FileUtils.startPhotoZoom(this, srcFile, outPutFile, REQ_ZOOM);// 发起裁剪请求 break; case REQ_ALBUM:// 选择相册中的图片 if (data != null) { Uri sourceUri = data.getData(); String[] proj = {MediaStore.Images.Media.DATA}; // 好像是android多媒体数据库的封装接口,具体的看Android文档 Cursor cursor = managedQuery(sourceUri, proj, null, null, null); // 按我个人理解 这个是获得用户选择的图片的索引值 int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); // 将光标移至开头 ,这个很重要,不小心很容易引起越界 cursor.moveToFirst(); // 最后根据索引值获取图片路径 imgPath = cursor.getString(column_index); srcFile = new File(imgPath); outPutFile = new File(FileUtils.generateImgePathInStoragePath()); outputUri = Uri.fromFile(outPutFile); FileUtils.startPhotoZoom(this, srcFile, outPutFile, REQ_ZOOM);// 发起裁剪请求 } break; case REQ_ZOOM://裁剪后回调 // Bundle extras = data.getExtras(); if (data != null) { // bm = extras.getParcelable("data"); if (outputUri != null) { bm = ImageTools.decodeUriAsBitmap(outputUri); //如果是拍照的,删除临时文件 temFile = new File(imgPath); if (temFile.exists()) { temFile.delete(); } //进行上传,上传成功后显示新图片,这里不演示上传的逻辑,上传只需将scaleImgPath路径下的文件上传即可。 //复制并压缩到自己的目录并压缩 scaleImgPath = FileUtils.saveBitmapByQuality(bm, 80); //获取图片名字 ImageGName(); //图片上传 HttpUtils.doFile("https://www.baidu.com", scaleImgPath,fileName , new Callback() { @Override public void onFailure(Call call, IOException e) { L.d("11111111111","### fileName : " + fileName); L.d("11111111111","失败"); } @Override public void onResponse(Call call, Response response) throws IOException { L.d("33333333333","### fileName : " + fileName); L.d("33333333333","成功"); } }); //显示在iv上 Glide.with(this) .load(outputUri) .placeholder(R.mipmap.ic_launcher) .crossFade() .into(ivPhoto); // ivPhoto.setImageBitmap(bm);//显示在iv上 } } else { UIUtils.showToast("选择图片发生错误,图片可能已经移位或删除"); } break; } break; }}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case 1: if (grantResults.length > 0) { List deniedPermissions = new ArrayList<>(); for (int i = 0; i < grantResults.length; i++) { int grantResult = grantResults[i]; String permission = permissions[i]; if (grantResult != PackageManager.PERMISSION_GRANTED) { deniedPermissions.add(permission); } } if (deniedPermissions.isEmpty()) { permissionListener.onGranted(); } else { permissionListener.onDenied(deniedPermissions); } } break; }} 复制代码
public void ImageGName(){ // 创建File File mFile = new File(scaleImgPath); // 取得文件名 fileName = mFile.getName();}复制代码