本例的SDKversion:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
plugins { id 'com.android.application' } android { compileSdkVersion 32 buildToolsVersion "30.0.3" defaultConfig { applicationId "com.cuiweiyou.piccomp" minSdkVersion 26 targetSdkVersion 32 versionCode 1 versionName "1.0" } } |
AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cuiweiyou.piccomp"> <!-- 对应权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application> <activity> </activity> <activity/> <!-- 1. name固定 authorities属性表示授权者,通常格式为:[appId].fileprovider; grantUriPermission是对uri授权权限,必须为true。 exported由于源代码设置只能为false; 2. 文件读写必须使用FileProvider操作 3. Intent中添加权限 --> <provider android:name="androidx.core.content.FileProvider" android:authorities="com.cuiweiyou.piccomp.fileprovider" android:exported="false" android:grantUriPermissions="true"> <!-- 4.file_paths中设置可访问的目录 --> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> </application> </manifest> |
src/main/res/xml/file_paths.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
<?xml version="1.0" encoding="utf-8"?> <resources> <paths> <!-- 这些是你在app中须要用到的目录,或者调起的app须要的。 首先 根据需要进行请求权限,并File进行创建 然后,使用时FileProvider操作。 name是自定义的,FileProvider操作后自动把真实的file路径转为这个名称路径。 path即实际要使用的文件路径。 path用一个点,即对应api返回的根路径;加斜杠及文件夹名,则须要先存在这个完整路径。 --> <!-- SD卡上APP的缓存目录,对外可见(通过文件管理器能找到),当用户卸载App的时候也会被删除。 api:Context.getExternalCacheDir() File路径:sdcard/Android/data/包名/cache/ FileProvider的uri路径 content://包.fileprovider/what_ever_name_ext_app_cache/ 【本例】 ./crop 即,在 sdcard/Android/data/包名/cache/ 下要先创建一个crop文件夹。 --> <external-cache-path name="what_ever_name_ext_app_cache" path="./crop" /> <!-- SD卡根目录下的APP文件目录,对外可见: 当用户卸载App的时候也会被删除 api:Context.getExternalFilesDir() 路径:sdcard/android/data/包名/file/ --> <external-files-path name="what_ever_name_ext_app_file" path="." /> <!-- 应用默认缓存根目录: api:Context.getCacheDir() 路径:data/data/包名/cache/ --> <cache-path name="what_ever_name_cache" path="." /> <!-- 应用目录: api:Context.getFilesDir() 路径:data/data/包名/file/ --> <files-path name="what_ever_name_file" path="." /> <!-- SD卡根目录: api:Environment.getExternalStorageDirectory() 路径:/storage/emulate/0 --> <external-path name="what_ever_name_ext_root" path="." /> <!-- 设备的根目录: api:Environment.getExternalStorageDirectory() 路径:/ --> <root-path name="what_ever_name_root" path="." /> </paths> </resources> |
文件工具类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class FileUtil { private static String crop_path; private FileUtil() { } // 临时目录 public static String getCropPath() { if (null == crop_path) { // 这个路径必须匹配 AndroidManifest.xml的 provider的 meta-data的 resource的 file_paths中的 external-cache-path crop_path = BaseApplication.getContext().getExternalCacheDir().getAbsolutePath() + "/crop/"; File cp = new File(crop_path); if (!cp.exists()) { cp.mkdirs(); } } return crop_path; } } |
在Intent中使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
String imgPath; // 全局变量 private void photoClip(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); // 调用系统中自带的图片剪裁 intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); // [1]加载图片用read intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("scale", true); intent.putExtra("noFaceDetection", true); imgPath = FileUtil.getCropPath() + System.currentTimeMillis() + ".jpg"; // [2]这里的文件路径 Uri cropUri_1 = getUri(this, imgPath); // 这里通过FileProvider将文件path转为uri。 intent.putExtra(MediaStore.EXTRA_OUTPUT, cropUri_1); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); // [3]FileProvider授权 List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); for (ResolveInfo resolveInfo : resInfoList) { String packageName = resolveInfo.activityInfo.packageName; grantUriPermission(packageName, cropUri_1, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); // 裁剪后保存用write } startActivityForResult(intent, CropRequestCode); } // 这里通过FileProvider将文件path转为uri。 private Uri getUri(Context context, String path) { File file = new File(path); if (file != null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // 第二个参数authority,对应AndroidManifest.xml的 provider的 authorities return FileProvider.getUriForFile(context, "com.cuiweiyou.piccomp.fileprovider", file); } else { return Uri.fromFile(file); } } return Uri.EMPTY; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == CropRequestCode && resultCode == RESULT_OK) { // 裁剪图片返回 Log.e("裁剪返回的图片", imgPath); } } |
- end
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/4128.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设