简体   繁体   English

按下按钮时从资产或原始文件夹中打开 PDF 文件的代码

[英]Code to open a PDF file from within either the assets or raw folder when a button is pressed

Found quite a few answers on stackoverflow related to this question but I haven't been able to implement any of them successfully.在与此问题相关的 stackoverflow 上找到了很多答案,但我无法成功实现其中任何一个。 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.我猜这是因为人们发布了特定的代码位,而我对 Java 的了解还不够多,无法修改它以适合我的应用程序。

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).我要的是打开保存在原始文件夹中的 PDF 文件所需的特定代码行(最初是资产文件夹,但这里的答案说资产文件夹中的项目被压缩,PDF 文件应该转到原始文件夹)文件夹)。

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.这里的很多答案还向您展示了如何在应用程序中打开它(使用库或 WebView),或者在应用程序启动时自动打开它。 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).我希望它通过意图在按钮单击时打开(因此它可以使用设备上已有的第三方 PDF 查看器打开)。

I've created a simple app in Android Studio with a single activity and a single button.我在 Android Studio 中创建了一个简单的应用程序,只有一个活动和一个按钮。 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).我还在每个文件夹(资产和原始)中保存了一个 PDF 文件。 Please show me how to proceed.请告诉我如何进行。

MainActivity.java:主活动.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:活动_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.我一直在寻找答案超过 4 天,但无济于事。

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最初是资产文件夹,但这里的答案说资产文件夹中的项目被压缩,PDF 文件应该转到原始文件夹

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 .这是当今的惯例,假设您所说的内部存储是指 Android SDK 所指的内部存储 You would then use FileProvider to make the PDF available to PDF viewer apps.然后,您将使用FileProvider使 PDF 可用于 PDF 查看器应用程序。

I have also saved a PDF file in each folder (assets and raw)我还在每个文件夹(资产和原始)中保存了一个 PDF 文件

Use getAssets().open(...) on a Context (eg, an Activity ) to get an InputStream on the asset, where ... is the relative path within assets/ to the PDF.Context (例如,一个Activity getAssets().open(...)上使用getAssets().open(...)来获取资产上的InputStream ,其中...assets/到 PDF 的相对路径。 So, if your PDF is in assets/doc.pdf , pass "doc.pdf" to open() .因此,如果您的 PDF 在assets/doc.pdf"doc.pdf"传递给open()

Or, use getResources().openRawResource() on a Context to get an InputStream on your raw resource.或者,在Context上使用getResources().openRawResource()来获取原始资源上的InputStream

Copying the PDF to a local file is then a matter of:将 PDF 复制到本地文件是一个问题:

  • Creating a File object on your desired location (eg, new File(getFilesDir(), "doc.pdf")在您想要的位置创建一个File对象(例如, new File(getFilesDir(), "doc.pdf")

  • Creating a FileOutputStream for that file为该文件创建一个FileOutputStream

  • Copying the bytes from the InputStream to the FileOutputStream , using standard Java file I/O使用标准 Java 文件 I/O 将字节从InputStream复制到FileOutputStream

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.然后,您可以配置FileProvider (或子类)以从您保存的任何位置提供 PDF,生成Uri ,并制作ACTION_VIEW Intent以打开 PDF 查看器。

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. 这是一个完整的示例应用程序,演示了所有这些,尽管我在启动 Activity 时启动 PDF 查看器,而不是在单击按钮时启动。

It is a little bit late but, this code piece's path is raw and you can only reach pdf files with this code.有点晚了,但是,此代码段的路径是原始的,您只能使用此代码访问 pdf 文件。

I hope it helps some我希望它可以帮助一些

 private fun openPDFFile() {
    
            val intent = Intent("android.intent.action.GET_CONTENT")
            intent.type = "application/pdf"
            startActivityForResult(intent, OPERATION_CHOOSE_PDF)
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM