1.引入包
//引入阿里云oss包 implementation 'com.aliyun.dpa:oss-android-sdk:+' implementation 'com.squareup.okhttp3:okhttp:3.10.0' implementation 'com.squareup.okio:okio:1.9.0' implementation 'com.android.support:multidex:1.0.3' implementation 'com.youth.banner:banner:1.4.10'
2.写入打开相机权限和写入SD卡的权限,网络权限
<!-- 往SDCard写入数据权限 --> <uses-permission android:name="android.permission. WRITE_EXTERNAL_STORAGE" /> <!--请求访问使用照相设备--> <uses-permission android:name="android.permission.CAMERA" />
3.写入打开相机的代码
private static final int CODE_GALLERY_REQUEST = 0xa0; private static final int CODE_CAMERA_REQUEST = 0xa1; private static final int CODE_RESULT_REQUEST = 0xa2; private static final int CAMERA_PERMISSIONS_REQUEST_CODE = 0x03; private static final int STORAGE_PERMISSIONS_REQUEST_CODE = 0x04; private File fileUri = new File(Environment.getExternalStorageDirectory().getPath() + "/photo.jpg"); private File fileCropUri = new File(Environment.getExternalStorageDirectory().getPath() + "/crop_photo.jpg"); private Uri imageUri; private Uri cropImageUri; /** * 自动获取相机权限 */ private void autoObtainCameraPermission() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) { ToastUtils.shortToast( "您已经拒绝过一次"); } ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, CAMERA_PERMISSIONS_REQUEST_CODE); } else {//有权限直接调用系统相机拍照 if (hasSdcard()) { imageUri = Uri.fromFile(fileUri); //通过FileProvider创建一个content类型的Uri if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { imageUri = FileProvider.getUriForFile(SettingActivity.this, "com.zz.fileprovider", fileUri); } PhotoUtils.takePicture(this, imageUri, CODE_CAMERA_REQUEST); } else { ToastUtils.shortToast( "设备没有SD卡!"); } } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { //调用系统相机申请拍照权限回调 case CAMERA_PERMISSIONS_REQUEST_CODE: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (hasSdcard()) { imageUri = Uri.fromFile(fileUri); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) imageUri = FileProvider.getUriForFile(SettingActivity.this, "com.zz.fileprovider", fileUri);//通过FileProvider创建一个content类型的Uri PhotoUtils.takePicture(this, imageUri, CODE_CAMERA_REQUEST); } else { ToastUtils.shortToast( "设备没有SD卡!"); } } else { ToastUtils.shortToast( "请允许打开相机!!"); } break; } //调用系统相册申请Sdcard权限回调 case STORAGE_PERMISSIONS_REQUEST_CODE: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { PhotoUtils.openPic(this, CODE_GALLERY_REQUEST); } else { ToastUtils.shortToast( "请允许打操作SDCard!!"); } break; default: } } private static final int OUTPUT_X = 1000; private static final int OUTPUT_Y = 1000; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { switch (requestCode) { //拍照完成回调 case CODE_CAMERA_REQUEST: cropImageUri = Uri.fromFile(fileCropUri); Log.e("显示",cropImageUri+""); PhotoUtils.cropImageUri(this, imageUri, cropImageUri, 1000, 1000, OUTPUT_X, OUTPUT_Y, CODE_RESULT_REQUEST); break; //访问相册完成回调 case CODE_GALLERY_REQUEST: if (hasSdcard()) { cropImageUri = Uri.fromFile(fileCropUri); Uri newUri = Uri.parse(PhotoUtils.getPath(this, data.getData())); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { newUri = FileProvider.getUriForFile(this, "com.zz.fileprovider", new File(newUri.getPath())); } PhotoUtils.cropImageUri(this, newUri, cropImageUri, 1, 1, OUTPUT_X, OUTPUT_Y, CODE_RESULT_REQUEST); } else { ToastUtils.shortToast( "设备没有SD卡!"); } break; case CODE_RESULT_REQUEST: Bitmap bitmap = PhotoUtils.getBitmapFromUri(cropImageUri, this); if (bitmap != null) { beginupload(cropImageUri); showImages(bitmap); } break; default: } } } /** * 自动获取sdk权限 */ private void autoObtainStoragePermission() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSIONS_REQUEST_CODE); } else { PhotoUtils.openPic(this, CODE_GALLERY_REQUEST); } } private void showImages(Bitmap bitmap) { cir_headimg.setImageBitmap(bitmap); } /** * 检查设备是否存在SDCard的工具方法 */ public static boolean hasSdcard() { String state = Environment.getExternalStorageState(); return state.equals(Environment.MEDIA_MOUNTED); }
4.上传图片到阿里云oss,使用下面方法必须继承的是activity类
public void beginupload(Uri bitmap) { Log.e("图片地址",bitmap+""); final String endpoint = "oss地址"; final String startpoint = "仓库名字"; // 明文设置secret的方式建议只在测试时使用,更多鉴权模式请参考后面的`访问控制`章节 OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider("keyid", "keysecret"); OSS oss = new OSSClient(getApplicationContext(), endpoint, credentialProvider); //通过填写文件名形成objectname,通过这个名字指定上传和下载的文件 // 构造上传请求 final String objectname = TimeUtil.getTimeMis() + ""; final String url = startpoint +"."+ endpoint+"/"+ objectname; PutObjectRequest put = new PutObjectRequest(startpoint, objectname, UiUtils.getImageAbsolutePath(SettingActivity.this,bitmap) ); // 异步上传时可以设置进度回调 put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() { @Override public void onProgress(PutObjectRequest request, long currentSize, long totalSize) { } }); OSSAsyncTask task = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() { @Override public void onSuccess(PutObjectRequest request, PutObjectResult result) { //上传成功后调用自己写的传到项目接口的方法 sethead(objectname); } @Override public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) { // 请求异常 if (clientExcepion != null) { // 本地异常如网络异常等 clientExcepion.printStackTrace(); } if (serviceException != null) { } } }); }
到这里,哪里使用直接调用打开相机或手机本地相册方法就可以了