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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…