#516·TakePhoto

和已有FileProvider共存问题(应该是最后的解决方式了)-希望git主能采用

Author: Cong2011Created Jul 12, 2019Updated Jun 18, 2021

如果可以自定义FileProvider和对应file_paths.xml文件的,看 #486 这篇,完美解决。 我是自己搭了个通用型lib框架,file_paths.xml名字可以改,但name不想叫camera_photos,但是源码里是写死的,于是改了git主源码,动态获取。

1、自己的改了名的file_paths.xml,paths标签下的一定只留下root-path标签,name随便(path得看你有没有root权限了)。`

2、修改takephoto_library里的tiul.TuriParse.java 122行 `

    // TODO: 19/7/12 Cong2011修改-动态替换uri头
    String[] np = getRootNamePath(context);
    path = new File(uri.getPath().replace(np[0] + "/", TextUtils.isEmpty(np[1]) ? "" : np[1] + "/")).getAbsolutePath();
//     path = new File(uri.getPath().replace("camera_photos/", "")).getAbsolutePath();`

`

private static String[] rootPath;

private static String[] getRootNamePath(Context context) {
    if (rootPath != null) return rootPath;
    try {
        final String authority = TConstant.getFileProviderName(context);
        final ProviderInfo info = context.getPackageManager()
                .resolveContentProvider(authority, PackageManager.GET_META_DATA);
        final XmlResourceParser in = info.loadXmlMetaData(
                context.getPackageManager(), "android.support.FILE_PROVIDER_PATHS");
        if (in == null) {
            throw new IllegalArgumentException(
                    "Missing " + "android.support.FILE_PROVIDER_PATHS" + " meta-data");
        }
        int type;
        while ((type = in.next()) != END_DOCUMENT) {
            if (type != START_TAG) continue;
            final String tag = in.getName();
            final String name = in.getAttributeValue(null, "name");
            String path = in.getAttributeValue(null, "path");
            if ("root-path".equals(tag)) {
                return rootPath = new String[]{name, path};
            }
        }
        throw new IllegalStateException("FILE_PROVIDER_PATHS 中未配制root-path标签");
    } catch (XmlPullParserException | IOException e) {
        e.printStackTrace();
        throw new IllegalArgumentException(
                "Missing " + "android.support.FILE_PROVIDER_PATHS" + " meta-data");
    }
}

`