Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
496 views
in Technique[技术] by (71.8m points)

android - java.lang.IllegalArgumentException: Failed to find configured root that contains /Download/Pdf_list/invoice.pdf

I want to open pdf from download folder, but I am getting this error

Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
    at android.view.View.performClick(View.java:7254)?
    at android.view.View.performClickInternal(View.java:7212)?
    at android.view.View.access$3800(View.java:828)?
    at android.view.View$PerformClick.run(View.java:27889)?
    at android.os.Handler.handleCallback(Handler.java:883)?
    at android.os.Handler.dispatchMessage(Handler.java:100)?
    at android.os.Looper.loop(Looper.java:227)?
    at android.app.ActivityThread.main(ActivityThread.java:7822)?
    at java.lang.reflect.Method.invoke(Native Method)?
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)?
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1026)?
 Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /Download/Pdf_list/invoice.pdf
    at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744)
    at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
    at com.example.jpegtopdf.MainActivity.MakePdf(MainActivity.kt:257)

I added provider in the manifest before for storing images in my app and now for opening pdf I had to make other provider here is the manifest file This provider is for image saving

<provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.example.jpegtopdf.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>

And this is for opening pdf

<provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.example.jpegtopdf.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
    </provider>

This is provider_paths xml file

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="external_files" path="."/>
</paths>

This is how I tried to open pdf through intent

val file = File(Environment.DIRECTORY_DOWNLOADS+"/Pdf_list","invoice.pdf")
val intent = Intent(Intent.ACTION_VIEW)
intent.flags = Intent.FLAG_ACTIVITY_NO_HISTORY
Intent.FLAG_ACTIVITY_NEW_TASK
val uri = FileProvider.getUriForFile(this, "com.example.jpegtopdf" + 
".fileprovider",file)
intent.setDataAndType(uri, "application/pdf")
intent.flags=Intent.FLAG_GRANT_READ_URI_PERMISSION
intent.flags=Intent.FLAG_GRANT_WRITE_URI_PERMISSION
this.startActivity(intent)

The file I want to open is in Download/Pdf_list/file_name

question from:https://stackoverflow.com/questions/65835026/java-lang-illegalargumentexception-failed-to-find-configured-root-that-contains

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...