Android 7.0 FileProviderが踏んだ穴

4352 ワード

前言:7.0システムの後にFileProviderを使用して発生した2つの問題を記録します.
問題1:
Error:C:***AndroidManifest.xml:352:13-62 Error:
	Attribute provider#android.support.v4.content.FileProvider@authorities value=(***.fileProvider) from AndroidManifest.xml:352:13-62
	is also present at [com.jph.takephoto:takephoto_library:4.0.3] AndroidManifest.xml:19:13-64 value=(***.fileprovider).
	Suggestion: add 'tools:replace="android:authorities"' to  element at AndroidManifest.xml:350:9-358:20 to override.
FAILURE: Build failed with an exception.

インベントリファイルの構成は次のとおりです.
   
            
        

この問題の上のエラーメッセージの中で実はすでにはっきり言って、繰り返し追加します
android:authorities="***.fileProvider"

私のプロジェクトにサードパーティ製のライブラリが追加され、サードパーティ製のライブラリのリストファイルにもこのコードが追加され、競合が発生したためです.
解決策は、エラー・ログにも示されています.独自のインベントリ・ファイルに次のコードを追加します.
xmlns:tools="http://schemas.android.com/tools"
tools:replace="android:authorities"

追加されたコードは次のとおりです.
   
            
        

再コンパイルパス.
問題2:
          リストファイルにはFileProviderの登録と問題が修正されたコードが一致しています.
         コードの使用方法は次のとおりです.
         Uri uri = FileProvider.getUriForFile( this, "***.filerovider", mTempFile);
このコードが実行されるたびにエラーが発生し、appがクラッシュします.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
                                                                  at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:583)
                                                                  at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557)
                                                                  at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:399)

FileProvider.getUriForFile()に入ります.ソースを表示します.
/**
 * Return a content URI for a given {@link File}. Specific temporary
 * permissions for the content URI can be set with
 * {@link Context#grantUriPermission(String, Uri, int)}, or added
 * to an {@link Intent} by calling {@link Intent#setData(Uri) setData()} and then
 * {@link Intent#setFlags(int) setFlags()}; in both cases, the applicable flags are
 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION} and
 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION}. A FileProvider can only return a
 * content {@link Uri} for file paths defined in their <paths>
 * meta-data element. See the Class Overview for more information.
 *
 * @param context A {@link Context} for the current component.
 * @param authority The authority of a {@link FileProvider} defined in a
 *            {@code } element in your app's manifest.
 * @param file A {@link File} pointing to the filename for which you want a
 * content {@link Uri}.
 * @return A content URI for the file.
 * @throws IllegalArgumentException When the given {@link File} is outside
 * the paths supported by the provider.
 */
public static Uri getUriForFile(Context context, String authority, File file) {
    final PathStrategy strategy = getPathStrategy(context, authority);
    return strategy.getUriForFile(file);
}

主に2番目のパラメータの紹介を見て、大体の意味は:ここのパラメータはリストファイルの中の
android:authorities="***.fileProvider"

  。 , ,authorities , 。