https://kotlinlang.org logo
#android
Title
# android
j

J6ey

04/05/2020, 7:40 AM
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

Pablichjenkov

04/06/2020, 5:35 AM
Have you tried the new Storage Access Framework?
d

David Eriksson

04/06/2020, 5:24 PM
Maybe a stack trace would help?
j

J6ey

04/06/2020, 5:27 PM
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

David Eriksson

04/06/2020, 5:37 PM
Not a single hint in logcat?
j

J6ey

04/06/2020, 5:38 PM
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

David Eriksson

04/06/2020, 5:41 PM
What if you remove the "pdfUri" parameter to new Intent()?
Also it seems you have Java code in a Kotlin Slack 😉
j

J6ey

04/06/2020, 5:45 PM
Removed from intent, no dice. Yeah I apologize for that haha
d

David Eriksson

04/06/2020, 5:48 PM
So what does the URI look like before you send the intent?
d

David Eriksson

04/06/2020, 5:49 PM
That can't be the URI in the pdfUri parameter
j

J6ey

04/06/2020, 5:52 PM
How should the format be like?
d

David Eriksson

04/06/2020, 5:52 PM
Show the URI from the debugger 🙂
It should be
content://
something
j

J6ey

04/06/2020, 5:54 PM
Good point 🙂
<content://com.sony.screeners.screenersapp.provider/external_files/Download/la.pdf>
d

David Eriksson

04/06/2020, 5:57 PM
What does your
file_provider_paths.xml
file look like?
j

J6ey

04/06/2020, 5:58 PM
Copy code
<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="external_files" path="."/>
</paths>
d

David Eriksson

04/06/2020, 6:14 PM
Hmm. Maybe you should try to open file Uri yourself with openAssetFileDescriptor and see if you get an error?