Found quite a few answers on stackoverflow related to this question but I haven't been able to implement any of them successfully. I'm guess it's because people post specific bits of code and I don't know nearly enough Java to modify it to fit my app.
What I'm asking for is the specific lines of code that are needed to open a PDF file saved in the raw folder (was originally assets folder but an answer here said items in the assets folder get compressed and PDF files should go to the raw folder).
From the other answers I've gathered that the file first needs to be copied to internal storage for it to be viewable. That means giving it read and write storage permissions.
A lot of the answers here also show you how to open it within the app (using either libraries or WebView), or open it automatically when the app starts. I don't want either of those behaviors. I want it to open on button click via intents (so it opens with a third party PDF viewer already on the device).
I've created a simple app in Android Studio with a single activity and a single button. It displays a toast when pressed so I know it's properly set up. I have also saved a PDF file in each folder (assets and raw). Please show me how to proceed.
MainActivity.java:
public class MainActivity extends AppCompatActivity {
public void showPDF (View view) {
Toast.makeText(MainActivity.this, "button pressed", Toast.LENGTH_LONG).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}}
activity_main.xml:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="showPDF"
android:text="Button"
tools:layout_editor_absoluteX="135dp"
tools:layout_editor_absoluteY="171dp" />
Please help. I've been looking for an answer for over 4 days now without avail.
That's not usually a problem.
That is the convention nowadays, assuming that by internal storage you mean what the Android SDK refers to as internal storage. You would then use
FileProvider
to make the PDF available to PDF viewer apps.Use
getAssets().open(...)
on aContext
(e.g., anActivity
) to get anInputStream
on the asset, where...
is the relative path withinassets/
to the PDF. So, if your PDF is inassets/doc.pdf
, pass"doc.pdf"
toopen()
.Or, use
getResources().openRawResource()
on aContext
to get anInputStream
on your raw resource.Copying the PDF to a local file is then a matter of:
Creating a
File
object on your desired location (e.g.,new File(getFilesDir(), "doc.pdf")
Creating a
FileOutputStream
for that fileCopying the bytes from the
InputStream
to theFileOutputStream
, using standard Java file I/OYou can then configure
FileProvider
(or a subclass) to serve the PDF from wherever you saved it, generate theUri
, and craft theACTION_VIEW
Intent
to open a PDF viewer.Here is a complete sample app that demonstrates all of this, though I launch the PDF viewer as part of starting the activity, rather than on a button click.