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.