(1) Create a new module
Create a library
module and complete the registration of FileProvider in its AndroidManifest.xml. The code is written as:
<application> <provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.android7.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> </application>
Be careful, android:authorities
don't write hard, because the library may eventually be referenced by multiple projects, android:authorities
but it cannot be repeated. If the same is defined in two apps, the latter cannot be installed on the phone (authority conflict).
The same write file_paths
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <root-path name="root" path="" /> <files-path name="files" path="" /> <cache-path name="cache" path="" /> <external-path name="external" path="" /> <external-files-path name="external_file_path" path="" /> <external-cache-path name="external_cache_path" path="" /> </paths>
Finally, write a helper class, for example:
public class FileProvider7 { public static Uri getUriForFile(Context context, File file) { Uri fileUri = null; if (Build.VERSION.SDK_INT >= 24) { fileUri = getUriForFile24(context, file); } else { fileUri = Uri.fromFile (file); } return fileUri; } public static Uri getUriForFile24(Context context, File file) { Uri fileUri = android.support.v4.content.FileProvider.getUriForFile(context, context.getPackageName() + ".android7.fileprovider", file); return fileUri; } public static void setIntentDataAndType(Context context, Intent intent, String type, File file, boolean writeAble) { if (Build.VERSION.SDK_INT >= 24) { intent.setDataAndType(getUriForFile(context, file), type); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); if (writeAble) { intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); } } else { intent.setDataAndType(Uri.fromFile(file), type); } } }
You can add methods according to your needs.
Okay, so our little library is written
(2) use
If any project needs to adapt to 7.0, then you only need to reference the library like this, and then you only need to change one line of code to complete the adaptation, for example:
Photograph
public void takePhotoNoCompress(View view) { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getPackageManager()) != null) { String filename = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.CHINA) .format(new Date()) + ".png"; File file = new File(Environment.getExternalStorageDirectory(), filename); mCurrentPhotoPath = file.getAbsolutePath(); Uri fileUri = FileProvider7.getUriForFile (this, file); takePictureIntent.putExtra (MediaStore.EXTRA_OUTPUT, fileUri); startActivityForResult(takePictureIntent, REQUEST_CODE_TAKE_PHOTO); } }
Just need to change: Uri fileUri = FileProvider7.getUriForFile(this, file) ;
install apk
FileProvider7.setIntentDataAndType(this, intent, "application/vnd.android.package-archive", file, true);即可。
https://github.com/hongyangAndroid/FitAndroid7