按下按钮时从资源或原始文件夹中打开PDF文件的代码 [英] Code to open a PDF file from within either the assets or raw folder when a button is pressed

查看:143
本文介绍了按下按钮时从资源或原始文件夹中打开PDF文件的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在与该问题有关的stackoverflow上找到了很多答案,但是我还没有成功实现它们中的任何一个.我猜这是因为人们发布了一些特定的代码,而且我不知道几乎没有足够的Java来修改它以适合我的应用程序.

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.

我要问的是打开原始文件夹中保存的PDF文件所需的特定代码行(本来是资产文件夹,但这里的回答是说资产文件夹中的项目被压缩了,PDF文件应该去了到原始文件夹).

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.

这里的许多答案还向您展示了如何在应用程序中(使用库或WebView)打开它,或在应用程序启动时自动打开它.我不想要任何一种行为.我希望通过意图通过按钮单击来打开它(这样它就可以在设备上已有第三方PDF查看器的情况下打开).

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).

我已经在Android Studio中创建了一个简单的应用,其中包含一个活动和一个按钮.按下时它会显示一个吐司,所以我知道它的设置正确.我还在每个文件夹(资产和原始文件夹)中保存了一个PDF文件.请告诉我如何进行.

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" />

请帮助.我一直在寻找答案已有4天以上了.

Please help. I've been looking for an answer for over 4 days now without avail.

推荐答案

最初是资产文件夹,但此处的回答是说资产文件夹中的项目已压缩,PDF文件应转到原始文件夹

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 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.

我还在每个文件夹(资产和原始文件夹)中保存了一个PDF文件

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

Context(例如,Activity)上使用getAssets().open(...)以获得资产上的InputStream,其中...assets/中PDF的相对路径.因此,如果您的PDF位于assets/doc.pdf中,请将"doc.pdf"传递给open().

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().

或者,在Context上使用getResources().openRawResource()以获得原始资源上的InputStream.

Or, use getResources().openRawResource() on a Context to get an InputStream on your raw resource.

那么将PDF复制到本地文件就可以了:

Copying the PDF to a local file is then a matter of:

  • 在所需位置(例如new File(getFilesDir(), "doc.pdf")

为该文件创建FileOutputStream

使用标准Java文件I/O将字节从InputStream复制到FileOutputStream

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

然后,您可以配置FileProvider(或子类)以从保存位置提供PDF,生成Uri,并制作ACTION_VIEW Intent以打开PDF查看器.

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.

这是一个完整的示例应用程序演示了所有这些内容,尽管我在启动活动的过程中启动了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.

这篇关于按下按钮时从资源或原始文件夹中打开PDF文件的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆