FileProvider crash - npe attempting to invoke XmlResourceParser on a null String

前端 未结 9 1909
误落风尘
误落风尘 2020-11-28 21:38

This is a part of my manifest:




        
9条回答
  •  天涯浪人
    2020-11-28 21:49

    First, be sure that you provider android:authorities does not conflict with your other providers. Besides that you may choose any name for the last part of its name: "provider", "fileprovider" etc., but app crashes when there are more than one android:authorities listed, while documentation states that it allows multiple values listed.

    file:// scheme is now not allowed to be attached with Intent on targetSdkVersion >= 24 (Android N 7.0), only content:// is always passed for all devices (Android 5, 6 and 7). But we encountered that Xiaomi breaks this Google convention and sends file://, hence data.getData().getAuthority() gives empty string.

    final String uriScheme = currentUri.getScheme();
    if ("content".equals(uriScheme)) {
        // getting full file path (works with some providers, i.e. Gallery)
        path = FileUtils.getPath(getContext(), currentUri);
        if (path != null) {
             currentFile = new File(path);
        }
    } else if ("file".equals(uriScheme)) {
        // in a rare case we received file:// in currentUri, we need to:
        // 1. create new File variable from currentUri that looks like "file:///storage/emulated/0/download/50044382b.jpg"
        // 2. generate a proper content:// Uri for it
        currentFile = new File(currentUri.getPath());
        String authority = data.getData().getAuthority();
        if (authority != null && !authority.isEmpty()) {
            currentUri = FileProvider.getUriForFile(getActivity(), authority, currentFile);
        }
    } else {
        // throw exception
    }
    

    Also, the bug when FileProvider.getUriForFile() resulted in crash java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example/files/attachments/image.jpg was fixed in Android Support Library v24.2.0. The problem was that FileProvider.java did not see external-path folders.

提交回复
热议问题