Code to open a PDF file from within either the ass

2019-09-14 22:43发布

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.

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-14 23:32

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

That's not usually a problem.

From the other answers I've gathered that the file first needs to be copied to internal storage for it to be viewable.

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.

I have also saved a PDF file in each folder (assets and raw)

Use getAssets().open(...) on a Context (e.g., an Activity) to get an InputStream on the asset, where ... is the relative path within assets/ to the PDF. So, if your PDF is in assets/doc.pdf, pass "doc.pdf" to open().

Or, use getResources().openRawResource() on a Context to get an InputStream 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 file

  • Copying the bytes from the InputStream to the FileOutputStream, using standard Java file I/O

You can then configure FileProvider (or a subclass) to serve the PDF from wherever you saved it, generate the Uri, and craft the ACTION_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.

查看更多
登录 后发表回答