1由于Android的版本更新,现在有一个库试AndroidX 抛弃了v4, v7 这类版本了
Android 7 之后开始使用的FileProvider 试v4 包里的一个
但是在适配Android 10 之后,改用Androidx库, 将是
androidx.core.content.FileProvider;
在这个位置,在解析例如QQ和微信的时候,会发现网上提供的源码的 getFPUriToPath 函数失效,这是由于QQ和微信并没有使用新版的Androidx库中的FileProvider导致在这个函数中有一部验证的过程失效,目前我简单的办法就是去掉这一步验证。直观上看这一步验证不影响整体。
修改后的源码如下
package com.aiocw.aihome.easylauncher.common;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.net.Uri;
import androidx.core.content.FileProvider;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
public class ProviderTools {
public static String getFPUriToPath(Context context, Uri uri) {
try {
List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);
if (packs != null) {
String fileProviderClassName = FileProvider.class.getName();
for (PackageInfo pack : packs) {
ProviderInfo[] providers = pack.providers;
if (providers != null) {
for (ProviderInfo provider : providers) {
if (uri.getAuthority().equals(provider.authority)) {
// if (provider.name.equalsIgnoreCase(fileProviderClassName)) {
Class<FileProvider> fileProviderClass = FileProvider.class;
try {
Method getPathStrategy = fileProviderClass.getDeclaredMethod("getPathStrategy", Context.class, String.class);
getPathStrategy.setAccessible(true);
Object invoke = getPathStrategy.invoke(null, context, uri.getAuthority());
if (invoke != null) {
String PathStrategyStringClass = FileProvider.class.getName() + "$PathStrategy";
Class<?> PathStrategy = Class.forName(PathStrategyStringClass);
Method getFileForUri = PathStrategy.getDeclaredMethod("getFileForUri", Uri.class);
getFileForUri.setAccessible(true);
Object invoke1 = getFileForUri.invoke(invoke, uri);
if (invoke1 instanceof File) {
String filePath = ((File) invoke1).getAbsolutePath();
return filePath;
}
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
break;
// }
// break;
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}