gpt4 book ai didi

android - 华为设备上的 FileProvider getUriForFile() 错误

转载 作者:IT老高 更新时间:2023-10-28 21:47:02 26 4
gpt4 key购买 nike

在我的应用中使用 FileProvider.getUriForFile 时出现仅在华为设备上发生的异常:

Exception: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/<card name>/Android/data/<app package>/files/.export/2016-10-06 13-22-33.pdf
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(SourceFile:711)
at android.support.v4.content.FileProvider.getUriForFile(SourceFile:400)

这是我的 list 中文件提供程序的定义:

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths" />
</provider>

配置路径的资源文件:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="external_files" path="" />
</paths>

您知道这个问题的原因以及为什么它只发生在华为设备上吗?鉴于我没有华为设备,我该如何调试?

更新:

我在我的应用程序中添加了更多日志,但在这些设备上同时打印 ContextCompat.getExternalFilesDirscontext.getExternalFilesDir 时得到了一些不一致的结果:

ContextCompat.getExternalFilesDirs:
/storage/emulated/0/Android/data/<package>/files
/storage/sdcard1/Android/data/<package>/files

context.getExternalFilesDir:
/storage/sdcard1/Android/data/<package>/files

这与 ContextCompat.getExternalFilesDirs 的文档不一致,该文档指出 返回的第一个路径与 getExternalFilesDir(String) 相同

这解释了问题,因为我在代码中使用 context.getExternalFilesDirFileProvider 使用 ContextCompat.getExternalFilesDirs

最佳答案

Android N 的更新(保留下面的原始答案,并确认这种新方法在生产中有效):

正如您在更新中提到的,许多华为设备型号(例如 KIW-L24、ALE-L21、ALE-L02、PLK-L01 和其他各种型号)违反了 Android 对 ContextCompat#getExternalFilesDirs(String) 的调用契约(Contract)。 .而不是返回 Context#getExternalFilesDir(String) (即默认条目)作为数组中的第一个对象,而是返回第一个对象作为外部 SD 卡的路径(如果存在)。

如果违反此订购契约(Contract),这些带有外部 SD 卡的华为设备将崩溃并显示 IllegalArgumentException调用FileProvider#getUriForFile(Context, String, File)对于 external-files-path根。虽然您可以寻求多种解决方案来尝试处理此问题(例如编写自定义 FileProvider 实现),但我发现最简单的方法是发现此问题并且:

  • Pre-N:返回 Uri#fromFile(File) ,由于 FileUriExposedException,它不适用于 Android N 及更高版本
  • N:将文件复制到您的 cache-path (注意:如果在 UI 线程上完成,这可能会引入 ANR)然后返回 FileProvider#getUriForFile(Context, String, File)对于复制的文件(即完全避免错误)

可以在下面找到完成此操作的代码:

public class ContentUriProvider {

private static final String HUAWEI_MANUFACTURER = "Huawei";

public static Uri getUriForFile(@NonNull Context context, @NonNull String authority, @NonNull File file) {
if (HUAWEI_MANUFACTURER.equalsIgnoreCase(Build.MANUFACTURER)) {
Log.w(ContentUriProvider.class.getSimpleName(), "Using a Huawei device Increased likelihood of failure...");
try {
return FileProvider.getUriForFile(context, authority, file);
} catch (IllegalArgumentException e) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
Log.w(ContentUriProvider.class.getSimpleName(), "Returning Uri.fromFile to avoid Huawei 'external-files-path' bug for pre-N devices", e);
return Uri.fromFile(file);
} else {
Log.w(ContentUriProvider.class.getSimpleName(), "ANR Risk -- Copying the file the location cache to avoid Huawei 'external-files-path' bug for N+ devices", e);
// Note: Periodically clear this cache
final File cacheFolder = new File(context.getCacheDir(), HUAWEI_MANUFACTURER);
final File cacheLocation = new File(cacheFolder, file.getName());
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(file);
out = new FileOutputStream(cacheLocation); // appending output stream
IOUtils.copy(in, out);
Log.i(ContentUriProvider.class.getSimpleName(), "Completed Android N+ Huawei file copy. Attempting to return the cached file");
return FileProvider.getUriForFile(context, authority, cacheLocation);
} catch (IOException e1) {
Log.e(ContentUriProvider.class.getSimpleName(), "Failed to copy the Huawei file. Re-throwing exception", e1);
throw new IllegalArgumentException("Huawei devices are unsupported for Android N", e1);
} finally {
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
}
}
} else {
return FileProvider.getUriForFile(context, authority, file);
}
}

}

连同 file_provider_paths.xml :

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="public-files-path" path="." />
<cache-path name="private-cache-path" path="." />
</paths>

一旦您创建了这样的类,请将您的调用替换为:

FileProvider.getUriForFile(Context, String, File)

与:

ContentUriProvider.getUriForFile(Context, String, File)

坦率地说,我不认为这是一个特别优雅的解决方案,但它确实允许我们使用正式记录的 Android 行为而无需做任何过于激烈的事情(例如编写自定义 FileProvider 实现)。我已经在生产中对此进行了测试,因此我可以确认它可以解决这些华为崩溃问题。对我来说,这是最好的方法,因为我不想花太多时间来解决明显是制造商的缺陷。

从之前的华为设备更新到 Android N:

由于 FileUriExposedException,这不适用于 Android N 及更高版本,但我还没有遇到在 Android N 上出现这种错误配置的华为设备。

public class ContentUriProvider {

private static final String HUAWEI_MANUFACTURER = "Huawei";

public static Uri getUriForFile(@NonNull Context context, @NonNull String authority, @NonNull File file) {
if (HUAWEI_MANUFACTURER.equalsIgnoreCase(Build.MANUFACTURER) && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
Log.w(ContentUriProvider.class.getSimpleName(), "Using a Huawei device on pre-N. Increased likelihood of failure...");
try {
return FileProvider.getUriForFile(context, authority, file);
} catch (IllegalArgumentException e) {
Log.w(ContentUriProvider.class.getSimpleName(), "Returning Uri.fromFile to avoid Huawei 'external-files-path' bug", e);
return Uri.fromFile(file);
}
} else {
return FileProvider.getUriForFile(context, authority, file);
}
}
}

关于android - 华为设备上的 FileProvider getUriForFile() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39895579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com