Hey guys I am trying to open a pdf file from the D...
# android
j
Hey guys I am trying to open a pdf file from the Downloads folder, but when I try to it closes immediately.
Copy code
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "la.pdf");

Uri pdfUri = FileProvider.getUriForFile(this, getPackageName() + ".provider", file);
Intent intent = new Intent(Intent.ACTION_VIEW, pdfUri)
.setDataAndType(pdfUri, "application/pdf")                   
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
file.exists()
shows that the file exists. I am able to locate and open the file in the Download folder, but just not by code. Any idea?
stackoverflow 1
p
Have you tried the new Storage Access Framework?
d
Maybe a stack trace would help?
j
The behavior is the android default pdf opener opens the pdf file from my app, then immediately closes and switches back to my app. There is no crash or exception I can trace. Only thing I can verify is that it’s probably not able to read the file, or any file for that matter.
d
Not a single hint in logcat?
j
not a single hint 😔
I’m actually curious myself if anyone can programmatically open a pdf file in Downloads on their end. This may be a version issue for all I know. I’ve tried with Android 7-10
d
What if you remove the "pdfUri" parameter to new Intent()?
Also it seems you have Java code in a Kotlin Slack 😉
j
Removed from intent, no dice. Yeah I apologize for that haha
d
So what does the URI look like before you send the intent?
d
That can't be the URI in the pdfUri parameter
j
How should the format be like?
d
Show the URI from the debugger 🙂
It should be
content://
something
j
Good point 🙂
<content://com.sony.screeners.screenersapp.provider/external_files/Download/la.pdf>
d
What does your
file_provider_paths.xml
file look like?
j
Copy code
<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="external_files" path="."/>
</paths>
d
Hmm. Maybe you should try to open file Uri yourself with openAssetFileDescriptor and see if you get an error?