{"id":974,"date":"2023-03-11T20:33:01","date_gmt":"2023-03-11T12:33:01","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=974"},"modified":"2023-04-29T13:26:16","modified_gmt":"2023-04-29T05:26:16","slug":"android-7-uses-fileprovider-to-solve-fileuriexposedexception-exceptions-caused-by-file-uri","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/android-7-uses-fileprovider-to-solve-fileuriexposedexception-exceptions-caused-by-file-uri\/","title":{"rendered":"Android 7.0\u4f7f\u7528FileProvider\u89e3\u51b3file:\/\/ URI\u5f15\u8d77\u7684FileUriExposedException\u5f02\u5e38"},"content":{"rendered":"<h2>\u73b0\u8c61\u63cf\u8ff0<\/h2>\n<h3>Android 7.0\u4ee5\u524d\u7684\u7248\u672c<\/h3>\n<pre><code class=\"language-java\">Uri photoUri = Uri.fromFile(tempFile);\nIntent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);\ncameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);\nstartActivityForResult(cameraIntent, REQUEST_CODE_CAMERA);<\/code><\/pre>\n<p>File\u6587\u4ef6\u76f4\u63a5\u8f6c\u6362\u6210&quot;file:\/\/xxx\/xxx\/xxx&quot;\u7684uri\u683c\u5f0f<\/p>\n<p><!-- more --><\/p>\n<h3>Android 7.0\u53ca\u4ee5\u540e\u7684\u7248\u672c<\/h3>\n<p>\u5f53\u628atargetSdkVersion\u6307\u5b9a\u621024\u53ca\u4ee5\u4e0a\u5e76\u4e14\u5728API&gt;=24\u7684\u8bbe\u5907\u4e0a\u8fd0\u884c\u65f6\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5219\u4f1a\u51fa\u73b0<code>FileUriExposedException<\/code>\u5f02\u5e38<\/p>\n<pre><code>android.os.FileUriExposedException: file:\/\/\/storage\/emulated\/0\/europa\/DCIM\/Camera\/0_42_20180908_123018_review.jpg exposed beyond app through ClipData.Item.getUri()<\/code><\/pre>\n<h2>\u4ea7\u751f\u539f\u56e0<\/h2>\n<p>\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/developer.android.com\/reference\/android\/os\/FileUriExposedException.html\">https:\/\/developer.android.com\/reference\/android\/os\/FileUriExposedException.html<\/a><\/p>\n<p>Android\u4e0d\u518d\u5141\u8bb8\u5728App\u4e2d\u628a<code>file:\/\/Uri<\/code>\u66b4\u9732\u7ed9\u5176\u4ed6App\uff0c\u5305\u62ec\u4f46\u4e0d\u5c40\u9650\u4e8e\u901a\u8fc7Intent\u6216ClipData\u7b49\u65b9\u6cd5\u3002<\/p>\n<p>\u539f\u56e0\u5728\u4e8e\u4f7f\u7528file:\/\/Uri\u4f1a\u6709\u4e00\u4e9b\u98ce\u9669\uff0c\u6bd4\u5982\uff1a<\/p>\n<ul>\n<li>\u6587\u4ef6\u662f\u79c1\u6709\u7684\uff0c\u63a5\u6536file:\/\/Uri\u7684App\u65e0\u6cd5\u8bbf\u95ee\u8be5\u6587\u4ef6\u3002<\/li>\n<li>\u5728Android 6.0\u4e4b\u540e\u5f15\u5165\u8fd0\u884c\u65f6\u6743\u9650\uff0c\u5982\u679c\u63a5\u6536file:\/\/Uri\u7684App\u6ca1\u6709\u7533\u8bf7<code>READ_EXTERNAL_STORAGE<\/code>\u6743\u9650\uff0c\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\u4f1a\u5f15\u53d1\u5d29\u6e83\u3002<\/li>\n<\/ul>\n<p>\u56e0\u6b64\uff0cGoogle\u63d0\u4f9b\u4e86FileProvider\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u751f\u6210content:\/\/Uri\u6765\u66ff\u4ee3file:\/\/Uri\u3002<\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p>\u9996\u5148\u5728<code>AndroidManifest.xml<\/code>\u4e2d\u6dfb\u52a0<code>provider<\/code><\/p>\n<ul>\n<li>android:authorities \u7528\u6765\u6807\u8bc6provider\u7684\u552f\u4e00\u6807\u8bc6\uff0c\u5728\u540c\u4e00\u90e8\u624b\u673a\u4e0a\u4e00\u4e2a&quot;authority&quot;\u4e32\u53ea\u80fd\u88ab\u4e00\u4e2aApp\u4f7f\u7528\uff0c\u51b2\u7a81\u7684\u8bdd\u4f1a\u5bfc\u81f4App\u65e0\u6cd5\u5b89\u88c5\u3002<\/li>\n<li>android:exported \u5fc5\u987b\u8bbe\u7f6e\u6210false<\/li>\n<li>android:grantUriPermissions \u7528\u6765\u63a7\u5236\u5171\u4eab\u6587\u4ef6\u7684\u8bbf\u95ee\u6743\u9650\uff0c\u4e5f\u53ef\u4ee5\u5728Java\u4ee3\u7801\u4e2d\u8bbe\u7f6e<\/li>\n<\/ul>\n<pre><code class=\"language-xml\">&lt;manifest xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n    package=&quot;me.yezhou.lib.photo&quot; &gt;\n\n    &lt;application&gt;\n        &lt;provider\n            android:name=&quot;android.support.v4.content.FileProvider&quot;\n            android:authorities=&quot;${applicationId}.fileprovider&quot;\n            android:grantUriPermissions=&quot;true&quot;\n            android:exported=&quot;false&quot;&gt;\n            &lt;meta-data\n                android:name=&quot;android.support.FILE_PROVIDER_PATHS&quot;\n                android:resource=&quot;@xml\/file_paths&quot; \/&gt;\n        &lt;\/provider&gt;\n    &lt;\/application&gt;\n\n&lt;\/manifest&gt;<\/code><\/pre>\n<p><code>res\/xml\/provider_paths.xml<\/code> \u662f\u6307\u5b9a\u8def\u5f84\u548c\u8f6c\u6362\u89c4\u5219\uff0c\u5982<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;resources&gt;\n    &lt;paths&gt;\n        &lt;external-cache-path path=&quot;photos\/camera&quot; name=&quot;camera_photos&quot; \/&gt;\n        &lt;cache-path path=&quot;photos\/camera&quot; name=&quot;camera_photos&quot; \/&gt;\n    &lt;\/paths&gt;\n&lt;\/resources&gt;<\/code><\/pre>\n<paths>\u4e2d\u53ef\u4ee5\u5b9a\u4e49\u4ee5\u4e0b\u5b50\u8282\u70b9<\/p>\n<p>| \u5b50\u8282\u70b9 | \u5bf9\u5e94\u8def\u5f84 | \u4f8b\u5b50 |<br \/>\n| :&#8211; | :&#8211; | :&#8211; |<br \/>\n| files-path | Context.getFilesDir() |  |<br \/>\n| cache-path | Context.getCacheDir() |  |<br \/>\n| external-path | Environment.getExternalStorageDirectory() | \/storage\/emulated\/0\/ |<br \/>\n| external-files-path | Context.getExternalFilesDir(null) |  |<br \/>\n| external-cache-path | Context.getExternalCacheDir() |  |<\/p>\n<p>\u5047\u5982\u8981\u5c06\u76ee\u5f55 <code>file:\/\/\/storage\/emulated\/0\/appblog.cn\/photo\/<\/code> \u66ff\u6362\u4e3a <code>content:\/\/${android:authorities}\/photo_files\/<\/code><\/p>\n<p>\u90a3\u4e48\u914d\u7f6e\u5e94\u8be5\u5199\u6210<\/p>\n<p>&#8220;`xml<br \/>\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br \/>\n&lt;paths xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;&gt;<br \/>\n    &lt;external-path path=&quot;appblog.cn\/photo&quot; name=&quot;photo_files&quot; \/&gt;<br \/>\n&lt;\/paths&gt;<br \/>\n&#8220;`<\/p>\n<p>\u7136\u540e\u4fee\u6539\u4ee3\u7801<\/p>\n<p>&#8220;`java<br \/>\n\/\/Uri photoUri = Uri.fromFile(tempFile);<br \/>\nUri photoUri = FileProvider.getUriForFile(<br \/>\n                mContext,<br \/>\n                mActivity.getPackageName() + &quot;.fileprovider&quot;,<br \/>\n                tempFile);<br \/>\nIntent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);<br \/>\ncameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);<br \/>\nstartActivityForResult(cameraIntent, REQUEST_CODE_CAMERA);<br \/>\n&#8220;`<\/p>\n<p>## \u5e38\u89c1\u5f02\u5e38\u5904\u7406<\/p>\n<p>&#8220;`<br \/>\njava.lang.SecurityException: Provider must not be exported<br \/>\n&#8220;`<\/p>\n<p>\u89e3\u51b3\u65b9\u6848\uff1a<code>android:exported<\/code>\u5fc5\u987b\u8bbe\u7f6e\u6210<code>false<\/code><\/p>\n<p>&#8220;`<br \/>\nAttempt to invoke virtual method &#039;android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)&#039; on a null object reference<br \/>\n&#8220;`<\/p>\n<p>\u89e3\u51b3\u65b9\u6848\uff1a<code>AndroidManifest.xml<\/code>\u5904\u7684<code>android:authorities<\/code>\u5fc5\u987b\u8ddf<code>mActivity.getPackageName() + &quot;.fileprovider&quot;<\/code>\u4e00\u81f4<\/p>\n<p>## FileProvider<\/p>\n<p>### FileProvider\u4f7f\u7528<code>content:\/\/Uri<\/code>\u7684\u4f18\u70b9<\/p>\n<p>&#8211; \u53ef\u4ee5\u63a7\u5236\u5171\u4eab\u6587\u4ef6\u7684\u8bfb\u5199\u6743\u9650\uff0c\u53ea\u8981\u8c03\u7528<code>Intent.setFlags()<\/code>\u5c31\u53ef\u4ee5\u8bbe\u7f6e\u5bf9\u65b9App\u5bf9\u5171\u4eab\u6587\u4ef6\u7684\u8bbf\u95ee\u6743\u9650\uff0c\u5e76\u4e14\u8be5\u6743\u9650\u5728\u5bf9\u65b9App\u9000\u51fa\u540e\u81ea\u52a8\u5931\u6548\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u4f7f\u7528<code>file:\/\/Uri<\/code>\u65f6\u53ea\u80fd\u901a\u8fc7\u4fee\u6539\u6587\u4ef6\u7cfb\u7edf\u7684\u6743\u9650\u6765\u5b9e\u73b0\u8bbf\u95ee\u63a7\u5236\uff0c\u8bbf\u95ee\u63a7\u5236\u662f\u5bf9\u6240\u6709App\u90fd\u751f\u6548\u7684\uff0c\u4e0d\u80fd\u533a\u5206App\u3002<br \/>\n&#8211; \u53ef\u4ee5\u9690\u85cf\u5171\u4eab\u6587\u4ef6\u7684\u771f\u5b9e\u8def\u5f84\u3002<\/p>\n<p>### <code>file:\/\/<\/code>\u5230<code>content:\/\/<\/code>\u7684\u8f6c\u6362\u89c4\u5219<\/p>\n<p>&#8211; \u66ff\u6362\u524d\u7f00\uff1a\u628a<code>file:\/\/<\/code>\u66ff\u6362\u6210<code>content:\/\/${android:authorities}<\/code><br \/>\n&#8211; \u5339\u914d\u548c\u66ff\u6362\uff1a\u904d\u5386<paths>\u7684\u5b50\u8282\u70b9\uff0c\u627e\u5230\u6700\u5927\u80fd\u5339\u914dpath\u7684\u5b50\u8282\u70b9\uff0c\u7528name\u66ff\u6362\u6389\u6587\u4ef6\u8def\u5f84\u91cc\u6240\u5339\u914d\u7684\u5185\u5bb9<\/p>\n<p>### \u8bbe\u7f6e\u6587\u4ef6\u7684\u8bbf\u95ee\u6743\u9650<\/p>\n<p>\u6709\u4e24\u79cd\u8bbe\u7f6e\u6743\u9650\u7684\u529e\u6cd5\uff1a<\/p>\n<p>&#8211; \u8c03\u7528<code>Context.grantUriPermission(package, uri, modeFlags)<\/code>\u3002\u8fd9\u6837\u8bbe\u7f6e\u7684\u6743\u9650\u53ea\u6709\u5728\u624b\u52a8\u8c03\u7528<code>Context.revokeUriPermission(uri, modeFlags)<\/code>\u6216\u7cfb\u7edf\u91cd\u542f\u540e\u624d\u4f1a\u5931\u6548\u3002<br \/>\n&#8211; \u8c03\u7528<code>Intent.setFlags()<\/code>\u6765\u8bbe\u7f6e\u6743\u9650\u3002\u6743\u9650\u5931\u6548\u7684\u65f6\u673a\uff1a\u63a5\u6536Intent\u7684Activity\u6240\u5728\u7684stack\u9500\u6bc1\u65f6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u73b0\u8c61\u63cf\u8ff0 Android 7.0\u4ee5\u524d\u7684\u7248\u672c Uri photoUri = Uri.fromFile(tempF [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[267],"class_list":["post-974","post","type-post","status-publish","format-standard","hentry","category-android-basic","tag-uri"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/974","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=974"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/974\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}