FileProvider:错误:java.lang.IllegalArgumentException:无法找到包含以下内容的已配置根

时间:2018-07-20 09:04:58

标签: android typescript nativescript angular2-nativescript android-fileprovider

我发现自己在使用Android时遇到问题: 我正在为使用Nativescript(Angular)开发的应用程序工作。我需要下载一个较大的PDF文件(大于40 MB)和cacharlo。从WebView打开/下载它我无法使用缓存,我想下载它,然后从文件管理器中打开它。我发现自己在使用Android时遇到问题:

JS: Error in openFile:  Error: java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.package.com/files/document.pdf
JS:     android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:738)
JS:     android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:417)
JS:     com.tns.Runtime.callJSMethodNative(Native Method)
JS:     com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1101)
JS:     com.tns.Runtime.callJSMethodImpl(Runtime.java:983)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:970)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:954)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:946)
JS:     com.tns.gen.android.view.GestureDetector_SimpleOnGestureListener_vendor_1_842640_TapAndDoubleTapGestureListenerImpl.onSingleTapUp(GestureDetector_SimpleOnGestureListener_vendor_1_842640_TapAndDoubleTapGestureListenerImpl.java:17)
JS:     android.view.GestureDetector.onTouchEvent(GestureDetector.java:640)
JS:     android.support.v4.view.Gesture...

我已在 app / App_Resources / src / main / res / xml 文件夹中创建文件 provider_paths.xml

<?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>

并在 标签下添加 app / App_Resources / src / main / res / AndroidManifest.xml

    <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/provider_paths" /> </provider>

对于打开的PDF文件,我使用Android本机代码:

    var extension = params.url.split('.').pop();
    var title: string = params.title == undefined ? "Open file..." : params.title;
    let intent = new android.content.Intent(android.content.Intent.ACTION_VIEW);
    let context = application.android.currentContext;
    let mimeType = findExtension(extension);        intent.setDataAndType(android.support.v4.content.FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileprovider", new java.io.File(filePath)), mimeType);
    intent.setFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION);
    context.startActivity(android.content.Intent.createChooser(intent, title));

但不起作用。代码有什么问题?谢谢

0 个答案:

没有答案