废话不多说,想知道和之前区别,网上一堆,直接上实现代码
1、拍照
private String mImagePath = Environment.getExternalStorageDirectory()+"/meta/";
private void takePicture(){ Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (cameraIntent.resolveActivity(getPackageManager()) != null) { File path = new File(mImagePath); if(!path.exists()){ path.mkdir(); } mCurrentFile = new File(mImagePath, System.currentTimeMillis()+".jpg"); Uri uri = FileProvider.getUriForFile(mActivity, getPackageName()+".provider", mCurrentFile); Log.d(TAG,"filePath = "+uri.toString()); cameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri); startActivityForResult(cameraIntent, CODE_CAMERA_REQUEST); } }
重点为:
Uri uri = FileProvider.getUriForFile(mActivity, getPackageName()+".provider", mCurrentFile);
实现首先:主配置文件增加provider
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.cloudminds.meta.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/> </provider>
其次增加xml文件
provider_paths.xml
<?xml version="1.0" encoding="utf-8"?> <resource xmlns:android="http://schemas.android.com/apk/res/android"> <external-path path="meta" name="images" /> </resource>
可以实现拍照了
2、相册选图
private void choosePicture(){ Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); pickIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(pickIntent, CODE_GALLERY_REQUEST); }
3、裁剪
public void cropRawPhoto(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", output_X); intent.putExtra("outputY", output_Y); intent.putExtra("return-data", true); startActivityForResult(intent, CODE_RESULT_REQUEST); }
说明:在拍照完成后调用裁剪时
cropRawPhoto(FileProvider.getUriForFile(mActivity, BuildConfig.APPLICATION_ID + ".provider", mCurrentFile));
相册选图调用裁剪
Log.d(TAG,"CODE_GALLERY_REQUEST uri = "+data.getData()); cropRawPhoto(data.getData());
可以实现功能了,可以优化图片的处理